Find the longest word, I satisfy all conditions, but still fail


My “solution”,

   function findLongestWord(str) {

 var tim = str.split();
  var dav = 0;

  for (var i = 0; i < tim.length;) 
      if ( tim[i] > tim[1]) 
        tim.replace(tim[1], "");
          dav = tim[1].length;

     } else {
         tim.replace([0], tim[1]);
           tim.replace(tim[1], "");
             dav = tim[1].length;
  return dav;

I’m very new, if anyone can help me understand this issue, I would be very thankful.
I get the message “tim.replace is not a function.”, I understand what that means, but not why it’s relevant.
I know this isn’t the best way to do this challenge, but i have been getting “not a function” messages a lot, and I think I need to understand why before I move on.


It looks like you are missing the iterator on your for loop.

a for loop should look like:
for (var i = 0; i < num; i ++)

have you tried adding a console.log to see what tim[i] is returning?


Thanks for replying, I’m now reading up on how to use the console, i don’t know how to do that yet. I must have been confused, I thought only the middle part (“conditional”?) was required in the loop, and i could use the next line as a replacement for the iterator?
I think I may need to redo some of the java lessons…


string split returns an array - arrays do not have a replace method - often “not a function” is from a variable not being the expected type


should be var tim = str.split(" ");

Also, why are you comparing against tim[1] throughout?


None of the three expressions in the control statement of a for loop are required

for(;;) {
  console.log("an infinite loop due to empty control statement")

for(let i=0;;++i) {
  if(i >= 2)
  console.log("same as using i < 2 as guard in control statement")

let n=2
let i=0
for(;i < n;++i) {
  console.log("same as setting i=0 in initialization expression in control statement")

let n=2
for(let i=0;i < n;) {
  console.log("same as using ++i in update expression in control statement")

Potential Infinite Loop!

Thank you!! That was all ridiculously helpful, I eventually broke down and looked at the answer, this is what I had for an hour before that…

function findLongestWord(str) {

 var tim = str.split(" ");
  var dav = 0;

  for (; tim.length > 1;) 
      if (tim[0] > tim[1])  {
      tim.splice(1, 1);
      dav = tim[0].length;

     } else {
      dav = tim[0].length;
  return dav;

Lol, it’s the little things…


Sorry for the late response. I check tim[0] (first word) against tim[1] (second word),
then i make the bigger word the first in the line and erase the shorter word making the array
one shorter.


OK that makes sense - I didn’t take the time to analyze in detail and it just looked unusual.


Surely tim is a list of strings, so this is comparing the first string against the second, which JS will do fine, it will just give you wierd results:

"hi" > "hello"
"hi" > "ho"
"hello" > "hi"

You should be comparing the length, and you shouldn’t need to splice the array at all - you should just need to update dav if the length is longer. Missing out conditions in the for loop is super bad style as well; it’s exceptionally difficult to read


Hi @Andrew1,
I am pretty sure I’ve seen this post a while back, and I recall someone saying that there’s got to be an easier way to do this.

So, without giving the punchline away, I would suggest a better approach to this problem would be to turn the string into an array. ‘Sort’ the array based on word length, and grab the first item from the sorted array.

The final code is surprisingly compact.

Try it out and let us know how it goes.



I wrote about the effect of different kinds of string comparison on sorting

I should point out sorting is not needed to solve the longest word problem


The comparison I was talking about isn’t related to sorting: the OP is trying to find the longest word, comparing strings using > will not give the correct true/false value in this case because that doesn’t compare length.

Edit: Also Math.max(split on spaces -> map string length) I guess is simplest way


comparison is always related to sorting because by definition you can only compare things using inequalities if an order is defined - sorting provides a useful “big picture” to see and understand the effect of different orders that can be applied to the same set of objects


Nono, you misunderstand: the OPs code cannot work properly due to a fundamental misunderstanding about what is being compared. What I’m talking about has literally nothing to do with sorting, it’s related to an error with syntax.

This is valid JS:

"hola" > "hi"

And it is true, but it isn’t true due to “hola” being a longer string than “hi”, it isn’t comparing length

"hola" > "hello"

This is also true

The error appears on this line:

if (tim[0] > tim[1]) {

tim is a list of strings, so this comparison will produce garbage results


I just forgot to add the .length on the ends of tim[0] and tim[1].
lol i looked at it wondering what was wrong for an hour.