hey guys ,
I’ve written this code down there and it worked in first shot . But I was like it can’t be a good way and there should be a better way . do you have any idea about the performance of different solutions ?
Your code so far
function findLongestWordLength(str) {
let spacesIndex = [];
for (let i in str) {
if (str[i] == " ") {
spacesIndex.push(i);
}
}
spacesIndex.push(str.length)
console.log(spacesIndex,spacesIndex.length)
let SIclone = [...spacesIndex];
for (let j =1; j < SIclone.length ; j++){
SIclone[j] = spacesIndex[j] - spacesIndex[j-1]-1;
}
let ans = Math.max(...SIclone);
return ans
}
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/67.0.3396.99 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/find-the-longest-word-in-a-string