Hey,
I know this is a pretty basic task but I’ve been at it for like 2 days on and still can’t figure out the correct logic with all these fancy loops. So this i how I wrote it for now just to get it done and whatnot, the approach might not be the best of course, but I’m working with limited time here and wanted to cover as much as possible. it seems to pass all the tests currently. but is the below code really that bad or can I move on from here? just need some peace of mind here, hopefully please be honest with me
const findLongestWordLength = (sentence) => {
let longestWord = 0;
let currentWord = 0;
let nextWord = 0;
let diff = 0;
let regex = /[a-zA-Z]/;
let wordsArray = [];
let numbersArray = [];
let j = 0;
for (let i = 0; i < sentence.length; i++) {
if (sentence[i] === ' ') {
j++;
numbersArray[j] = i;
}
}
numbersArray.push(sentence.length);
for (let k = 0; k < numbersArray.length - 1; k++) {
currentWord = numbersArray[k] + 1;
nextWord = numbersArray[k + 1];
diff = nextWord - currentWord;
if (diff > longestWord) {
longestWord = diff;
}
}
return longestWord;
};
console.log(findLongestWordLength('The quick brown fox jumped over the lazy dog'));
