Build a Longest Word Finder App - Build a Longest Word Finder App

Tell us what’s happening:

I believe my solution for this lab is correct (albeit very long and ugly lol) but I am not sure what the problem is. The tests are saying that im failing here

  1. findLongestWordLength(“The quick brown fox jumped over the lazy dog”) should return a number.

but I checked with both typeOf() and isNan() for good measure, but both are saying that it is returning a number so I don’t know where I am going wrong. I would appreciate any advice

Your code so far

const findLongestWordLength = (sentence) => {
  let end = 0;
  let count = 0;
  let longest = 0;

  for (char of sentence) {
    end++;
    if (char === " ") {
      if (count > longest) {
        longest = count;
        count = 0;
      } else {
        count = 0;
      }
    } else if (end === sentence.length) {
      count++;
      if (count > longest) {
        longest = count;
      }
    } else {
      count++;
    }
  }

  return longest;
};

const test = findLongestWordLength(
  "What if we try a super-long word such as otorhinolaryngology"
);

console.log(
  test, // 19
  typeof test, // number
  isNaN(test) // false
);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:141.0) Gecko/20100101 Firefox/141.0

Challenge Information:

Build a Longest Word Finder App - Build a Longest Word Finder App
https://www.freecodecamp.org/learn/full-stack-developer/lab-longest-word-in-a-string/build-a-longest-word-finder-app

your code has an error that is being logged to the console.

to see it, make a small change in the code like remove a semicolon and add it back again.
Don’t run the tests or they will hide the error.

1 Like

found the problem! tysm

1 Like