BasicAlgorithm variable not changing


#1

I’m currently working on the BasicAlgorithm section of freeCodeCamp and im struggling with some projects because I want to return a variable at the end of my program but it is only returning the intial value and not the reassigned values. I am using the var function so it should be changing right? Here is an example of my code.

function findLongestWord(str) {
var myArray=[];
var longestWord=1;
myArray=str;
var myArraySplit=myArray.split(" ");
for(i=0,i<myArraySplit.length;i++;){
if (myArraySplit[i].length>longestWord){
longestWord=myArraySplit[i].length;
}
}
return longestWord;
}

This is returning 1 but I have checked the myArraySplit[i].length and it is displaying numbers greater than 1 so I know it is just a problem with the longestWord variable. It will always return the intial value it is assigned. Am i doing something wrong?


#2

Oh you were so very close to the solution .Everythig is right except for this line:for(i=0,i<myArraySplit.length;i++;) after i=0 you have put a comma instead of semi colon and afteri++ you have an extra semicolon.So change that line to for(i=0;i<myArraySplit.length;i++)


#3

It looks like that the problem begins with the fact that you’re calling myArray.split(). The split() method isn’t an Array method, it’s a string method. In fact, I don’t think you even need to create the variable myArray, your function already has a variable called str that you can use. It’s one of the arguments you set for your function. str should be a string that has the split() method.

Try changing var myArraySplit = myArray.split(" “); to var myArraySplit = str.split(” ");

Also, double check your for loop. There’s a comma that doesn’t belong there, and a semi colon at the end that shouldn’t be there. (Turn the comma into a semi-colon and get rid of the semi-colon at before the closing parenthesis.)

Hopefully this helps!