Tell us what’s happening:
Hi everybody!
This code of mine works for all strings except the last, with the last word which is the longest with 19 chars…can you please explain me why?
Your code so far
function findLongestWordLength(str) {
let count = 0;
let record = ;
let result = 0;
for(let i = 0; i < str.length; i++) {
if(str[i] === " "){
record.push(count);
count = 0;
}
else {
count++;
}
}
for(let k = 0; k < record.length; k++) {
if(record[k] > result) {
result = record[k];
}
}
return result;
}
findLongestWordLength(“The quick brown fox jumped over the lazy dog”);
function findLongestWordLength(str) {
let count = 0;
let record = [];
let result = 0;
for(let i = 0; i < str.length; i++) {
if(str[i] === " "){
record.push(count);
count = 0;
}
else {
count++;
}
}
for(let k = 0; k < record.length; k++) {
if(record[k] > result) {
result = record[k];
}
}
return result;
}
findLongestWordLength("The quick brown fox jumped over the lazy dog");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36
Challenge: Basic Algorithm Scripting - Find the Longest Word in a String
Link to the challenge: