Find the Longest Word in a String Passing all but Last

hey there basicly i wrote my code very differntly that the hints as i had a look over i personally didnt feel the need to split it up into arrays and count the arrays as remembering the methods is longer than just writing a couple of for loops however for some reason my code seems to only work for words up to the length of 10 is there something im not understanding in javascript?

Your code so far

function findLongestWordLength(str) {
  let count = 0;
  let loopCount = 0;
  let hold = 0
    for (let i = 0 ; i < str.length ; i++){
   //    console.log(loopCount)
       if (str[i] != " "){
        // console.log(count);
       } else if (str[i] === " "){
         if (count > hold){
           hold = count; 
         } else {}
         count = 0;

  return hold;

findLongestWordLength("hi there otorhinolaryngology");

Your browser information:

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

Link to the challenge:

what happens if the longest word is the last one in the string? this will not execute because the loop has already ended

1 Like

yeah i realised after typing i was wrong about the 10 thing and the code is exiting early when working on other langs ive had a tracker where it slows down the code and goes line per line with a click so i can kinda see it working and then debugging is much easyer why is it only effecting the last word of a string?

ok got it cause im looking for a space and there is no space at the end of the word does javascript have a null terminal i can check for as well to fix the code?

so i pased by adding an variable " " to the str passed but before that i tried things such as :

(str[i] === " " || i == str.length)

(str[i] === " " || str[i] = undefined / null) {as i cant find info on null terminal when i search goggle obviously i changed the strlength loop to go above the found charecters hoping to find some form of error}

i also made varibles loopCount and debug to incase passing the str.length or the i were out of scope in a way i didnt understand but non passed am i just being very bad at my understanding here the first OR statement to me should pass cause i is going to get to the length of str for the loop to end and for the program not to crash so surely it would get to the last coniditon of the loop check the if statement not pass for the space but pass for i == str.len then compare hold value to current value before returning the final highest hold value no?

the condition of the loop is i < str.length, so the last value i will get is str.length - 1 as with a value of str.length the body of the loop is not executed because the condition is false

i actually did try having my global loopCounter variable =1 and =2 and also tried inside the coniditon str.len-1 as in the reserve string challenge i used no methods and used str.length*2 to be able to go over and passed the manule array issues i came across which worked then but this time nether of these made any differnce maybe i just messed up but ill go back and try again to make that pass.