Trouble: Find the Longest Word in a String

Tell us what’s happening:
I’m getting z is not defined. I’ve defined it as a var so i don’t get why this error is turning up.

Your code so far


function findLongestWordLength(str) {
var x = str.split(' ');
console.log(x);
 var y = x[0];
 for (var i=0; i<x.length; i++)
 { var z = x[1+i];
   if (y.length < z.length)
   {
    y = z;
   } else {
     var y = x[i];
   }
 }
 console.log(y);
}


findLongestWordLength("The quick brown fox jumped over the lazy dog");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:77.0) Gecko/20100101 Firefox/77.0.

Challenge: Find the Longest Word in a String

Link to the challenge:

1 Like

It doesn’t count it as defined because it is defined inside the for loop. Try defining it outside a loop.

I defined it outside the for loop, still the same.

function findLongestWordLength(str) {
 var x = str.split(" ");
 console.log(x);
  var y = x[0];
  var z = '';
  for (var i=0; i<x.length; i++)
  {  z = x[1+i];
    if (y.length <= z.length)
    {
     y = z;
    } else {
      var y = x[0];
    }
  }
  console.log(y);
  console.log(y.length);
}

findLongestWordLength("Funny world it is");

I defined it outside the for loop, still shows the same error.

You don’t need z variable, just set y = 0; and compare if x[i].length > y {
y = x[i].length; }

Thanks! If you are able to find out why there was an undefined, please do share. I’m interested in knowing the reasons for such errors.

if you mean the error

Uncaught TypeError: Cannot read property 'length' of undefined

let’s consider this:
when you are in the last iteration of the loop, what’s the value of z?

Using console.log is a great necessity, you can try with something like this:

function findLongestWordLength(str) {
var x = str.split(' ');
 var y = x[0];
console.log({x, y});
 for (var i=0; i<x.length; i++)  {
   var z = x[1+i];
console.log({i, "x.length": x.length, z})
console.log({ "z.length": z.length, "y.length": y.length})
   if (y.length < z.length)
   {
    y = z;
   } else {
     var y = x[i];
   }
console.log({x, y, z});
 }
 console.log({y});
}
1 Like

@Athena,
to understand the error, consider the code below

var x =["a", "b", "c"]; 
// (i)ndexes  0, 1, 2

console.log(x[0]); // a
console.log(x[1]); // b
console.log(x[2]); // c
 
console.log(x[3]); // undefined
//at the end 3= 2+1 or i+1 or 1+i 
// and this does not exist in the array - 
// so the output is undefined


// same way at the end of iteration in the for loop
// your code is getting the error of undefined 
var z=x[1+i] // this is the main point to understand

I’ve edited your post for readability. 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.

Please 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.

markdown_Forums

1 Like