I don’t know what you mean by “forbidden”.
But OK, you have a working solution - at least it will if you return result from the function. I would describe it as:
Break the sentence into words.
Save the length of each of those words in and array.
Find the largest number in the array.
Return that number. <--- Missing this step.
In fact I wanted to check all numbers of the table and in case of j > “result”, to put it in the “result” variable but the problem is when the value is updated once, the program go out of the loop “for” and it’s normal.
I don’t understand. The for
loop is working normally for me.
If I run this code:
function findLongestWordLength(str) {
let tabWord = str.split(' ');
let tabletters = [];
let result = 0;
for (let i = 0; i < tabWord.length; i++) {
console.log('i =', i, ' --- tabword[i] = ', tabWord[i])
tabletters.push(tabWord[i].length);
}
console.log(tabletters);
for (let j = 0; j < tabletters.length; j++) {
console.log('j =', j, ' --- tabletters[j] = ', tabletters[j])
if (tabletters[j] > result) {
console.log('It\'s bigger, making the swap!')
result = tabletters[j];
}
}
console.log('result = ', result);
}
findLongestWordLength("Theeeee quick brown fox jumped over the lazy dogggggg");
the output is:
i = 0 --- tabword[i] = Theeeee
i = 1 --- tabword[i] = quick
i = 2 --- tabword[i] = brown
i = 3 --- tabword[i] = fox
i = 4 --- tabword[i] = jumped
i = 5 --- tabword[i] = over
i = 6 --- tabword[i] = the
i = 7 --- tabword[i] = lazy
i = 8 --- tabword[i] = dogggggg
(9) [7, 5, 5, 3, 6, 4, 3, 4, 8]
j = 0 --- tabletters[j] = 7
It's bigger, making the swap!
j = 1 --- tabletters[j] = 5
j = 2 --- tabletters[j] = 5
j = 3 --- tabletters[j] = 3
j = 4 --- tabletters[j] = 6
j = 5 --- tabletters[j] = 4
j = 6 --- tabletters[j] = 3
j = 7 --- tabletters[j] = 4
j = 8 --- tabletters[j] = 8
It's bigger, making the swap!
result = 8
That looks like it is working to me.