Hey, buddy, here’s the deal:
It checks while the letter array "nay" is undefined, that is, when it enters the rule of counting and starts to add up the values. The letter H in the case is defined, that is, opposite of what you are saying. and is not an array as you published, of the type: charAt[i]
, even behaving as such, this is a function of javascript, which captures the position of the letter in the string:
For example: 'Hello'.charAt(0) irá retornar "H";
The function String.charAt()
return the letter at the position of the string [0 => 'H', 1 => 'E', 2 => 'L', ...]
if in the case is testing the letter, consider that i is 0, then in the variable char
will have the letter "H", it is undefined, right, then it enters into Isis, the [denied+equal+equal] ( !== ) is checking if it is different both the type, and its value.
That is, if it is an "H" it enters the Else and receives the value 1:
let char = str.charAt(i);
`else arr[char] = 1;`
The while
is increasing the variable i , while it is less than the total of letters of the word, this causes it to traverse letter to letter and assign the values of the sum to defined elements, in all other situations will be undefined
, and will receive the value 1:
if (arr[char]!== undefined){
arr[char]++;
}
Note that while does this for each letter:
arr[char]
will add up when it is set:
arr["H"]++
arr["E"]++
arr["L"]++ //aqui fará uma soma de vezes pois ele terá sido definido mais de uma vez
arr["O"]++
He’ll go through the assignment rule five times:
First you will enter Isis, since arr[char]
not yet defined, returning the value 1 to the position of the letter H.
2nd will enter the E-this, since arr[char]
not yet defined, returning the value 1 to the position of the letter E.
3rd will enter the Else, since arr[char]
not yet defined, returning the value 1 to the position of the letter L.
4º enters the if condition, once it has already defined arr[char]
with the letter L, it will add up: 2
5º enters Else again because the letter O has not yet been defined and puts the value 1.
You can test and see it happen as follows in your browser console:
function letter_frequency(s) {
let arr = [];
let i = 0;
let str = s.toUpperCase();
while (i < str.length){
let char = str.charAt(i);
if (arr[char]!== undefined){
arr[char]++;
console.log('definido', arr[char], char)
} else {arr[char] = 1;
console.log('indefinido', arr[char], char)
}
i++
}
return arr;
}
letter_frequency("Hello");
JS has some weird things and I may not know one of them, but for me this is bad code and it doesn’t make sense. o
s
can beundefined
. The code is so bad it makes two loops where it could make one, and the person who made it didn’t realize it, must have put thisif
cheerful also because he saw something similar somewhere that manipulated another type of object.– Maniero
The misspelled ta code will never have the effect of counting frequency.
– novic
It’s not the letter H that’s being compared to
undefined
, and yesarr["H"]
. Basically,arr[letra]
keeps the quantity of that letter, but if it is the first occurrence of that letter,arr[letra]
it does not yet exist, so it is verified whether it isundefined
– hkotsubo
Now that I’ve seen that he’s using it as a dictionary.
– Maniero
I understood the question is not "why the code does not work" but "why the tested letter is Undefined"
– Augusto Vasques
It loops the string of the word and when it does not find a letter in the string, that is, while it nay for Undefined, meaning that the letter does not exist in the string, it defines that the frequency is 1 (once). Otherwise, it sums up the amount of times the letter repeats in the string, removes the Let and uses var... to understand: use: 'hello'. charAt(0); 'hello'. charAt(1);
– Ivan Ferrer
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.
– Maniero