Build a Longest Word Finder App

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'));

I’ve edited your post to improve the readability of the code and blurred it, so it doesn’t spoil the learning experience for other campers.

When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Would you post a link to this challenge please? Thank you.