Javascript Challenges - Count Repeating Letters

preview_player
Показать описание
Javascript Challenges - Count Repeating Letters

COUPONS BELOW!!!!!!

All courses for only $9.99!

Styled Components

React

Gatsby

Javascript

Bootstrap 4

HTML AND CSS

JQUERY

CSS GRID

CSS FLEXBOX

Products I Use:

Books I Recommend:

Disclosure: This video is not sponsored. Some links above are affiliate links, and l may earn a small commission from any purchases at no additional cost to you. Thank you for supporting my channel!
Рекомендации по теме
Комментарии
Автор

Thanks for this challenge idea. I came up with a somewhat different solution (before I watched yours), which also works when letters are mixed up:

function countLetters(str)
{
let obj = {};
str.split("").forEach(l => obj[l] ? obj[l]++ : obj[l] = 1);
return Object.entries(obj).reduce((total, entry) => total + entry[1] + entry[0], "");
}

bruslaw
Автор

Thanks a lot! Awesome as ever! Another simple approach
function countLetters(str) {
str += "#";
let letts = str[0];
let count = 0;
let result = "";
for (let char of str) {
if (letts === char) {
count++;
} else {
result += `${count}${letts}`;
letts = char;
count = 1;
}
}
return result;
}

bulatbaltin
Автор

You are awesome!!!
I watch all your videos!!!
What you are doing..is actually helping a lot of new developers out there!!!
Lots of Love from India❤️❤️❤️
Please make more JavaScript challenges vieeos!! :)

ayanbanerjee
Автор

What about the boundary case? When i = tempArray.length - 1
In that case comparing tempArray[i] with tempArray[i+1] will not give any error? Because index i+1 does not exist.
instead if condition should be if(temp[i] === temp[i+1] && i! = tempArray.length)

akhilsinghas
Автор

Thanks for the challenges list..
For this specific case, i resolved this in this way:

function onlyLetters(str) {
let onlyLettersStr =

let objStr = onlyLettersStr.reduce((acc, curr) => {
acc[curr] = acc[curr] ? acc[curr] + 1 : 1
return acc
}, {})

let result = '';
for(let key in objStr) {
result += `${objStr[key]}${key}`
}
return result
}

yamilzavala
Автор

it is count repeat of the next. not really repeat of whole string.

letoan
Автор

I enjoyed the explanation, wanted to ask how u could use this function to count duplicate numbers, then return the most frequent number?

kadirit
Автор

Hi John, I love to watch your work. this is my approach for the following challenge. I found a bug in your approach is that if we so the result would be "4f1e2f1e2r3t2o" but using my approach the answer is "6f2e2r3t2o"

function countLetters(str) {
const givenArr = str.split("");
const uniqueArr = [...new Set(str)];

let letters = []

for (let i = 0; i < uniqueArr.length; i++) {
let count = 0;
for (let j = 0; j < givenArr.length; j++) {
if (uniqueArr[i] === givenArr[j]) {
count++;
}
}
let value = `${count}${uniqueArr[i]}`;
letters= [...letters, value];
}

return letters;
}


codingbox
Автор

Thanks for the video. Can you please tell me which extension do you use for output console in the editor?

m.talalmalick
Автор

Thank you,
but why you don't use ES6+?

anastely
Автор

Nicely done, please, count repeating numbers challenges in an array

senderiandocaminos
Автор

What if the letters are mixed ex: aabbnaajkkaa, since you are just comparing it to the next value?

ProfessorLinux
Автор

where can I find challenges like this on yb or an another place mainly complex challenges?

richardgomez
Автор

Thank you for your efforts, I came up with a solution that doesn't require space and it works well.

function countLetters(str) {
let start = 0;
let track = 0;
let count = 0;
let result = "";

while (track <= str.length) {
if (str[track] === str[start]) {
count += 1;
track++;
} else {
result += `${count}${str[start]}`;
start = track;
count = 0;
}
}

return result;
}

anouarelmaaroufi
Автор

What is your extension showing console output?

dgknca
Автор

This is nice and all, but only if the letters are next to one another.

cdu
Автор

Can Anyone one help me out "Joan Smith" or "Matthew Joan Smith" how I will get only first and last word from these string.Answer must be : - for "Joan Smith" it will be (JS) and for "Matthew Joan Smith" it will be (MS).

singhvimlesh
Автор

Line num 13 little confusing can anyone make it clear??

sajidahmad
Автор

This will only work if the letters are in order

murshidhaniffa
Автор

const countLetters = (str) => {
let letters = [...new Set(str)];
let vals = str.split("");
let result = "";
const counter = (arr, lett) => {
let count = 0;
arr.map(obj => {
if (obj === lett) {
count++
};
});
result += (count + lett);
};
letters.map(lett => counter(vals, lett));
return result;
};

abdelkarimabouzi