Simple if else loop in js (syntax problem)

Simple if else loop in js (syntax problem)
0.0 0

#1

I do not understand which part of my code is wrong. (syntax wise)

var str="The quick brown fox jumped over the lazy dog";
function split(){return str.split(' ');}
split(str);

var str2 = split(str);
if (a === 0; a < 3; a++) {
  return (str[count].length);
} 

I am doing the “find out the longest word in a sentence” challenge. Logic wise I will worry about it later, and dont spoil it for me if you can, but the syntax is somehow wrong, at the if statement as well as return statement.

I dont know how to correct it.


#2

i just changed the if else loop into a for loop…

then what am I supposed to do?>

var str="The quick brown fox jumped over the lazy dog";
function split(){return str.split(' ');}
split(str);

var str2 = split(str);
for(a === 0; a < str2.length ; a++) {
  return str2[a].length;
} 

#3

cursory glance suggest a few problets (only little problems :wink:

  1. the split function returns an array but leave the parameter (str) unchanged, so the line following the function definition does nothing.
  2. Your if statement is confused with a for loop. The statements inside the brackets is for loop notation, so you’re mixing your toasties and I suspect an error is reported.
  3. you don’t use a return statement in an if statement, it is a simple condition followed by a statement block that is executed if the if the condition is true.

I’m afraid the code shown is rather mixed up and makes no sense. I suggest you break it down into smaller chunks and we can help you with those - at least if you don’t want us to just give you the answer.

When you break the problem down, write what you want to do as english, in comments if you like, then we can more easily see what each piece of code is supposed to do, and advise you on a piecemeal basis.

Hope this help and makes sense


#4

Hi @zhouxiang19910319

There’s two problems.

  1. The first part of the for loop statement a === 0 is used for initialization, your’re currently doing an equals check. Whilst not a syntax error. you’ll want to change that to: var a = 0. See here
  2. Inside the for loop you’re using the return statement. This is only a legal statement inside a function and this is will be where the syntax error is coming from.

#5

Regarding the return statement: As soon as you return the loop stops running. You need something to put the values into as the loop runs, then you return that thing afterwards (you’re counting length, so assuming an array, which you should push to inside the loop).

var whateverTheOutputShouldBe = []
for (var i = 0; i < something; i++) {
  whateverTheOutputShouldBe.push(i);
}
return whateverTheOutputShouldBe;

And if…else isn’t a loop, it’s a condition (if true do this else do that).


#6

my previous answer is now a little out of date given how you have updated the code, but it still doesn’t make sense.

Try writing the code out as english statements, as I suggest in my previous reply. Comment out the english statements and put the code following. We can then tell you which bits need attention.