1

I'm new to stackoverflow and I'm interested in learning javascript for fun. I've only been studying for about a couple of weeks so far and I'm trying to solve a coding exercise but I'm having a hard time doing it. Below is my code and I keep getting an error that states I'm missing a ( before a condition. I've looked it over several times and don't see where I'm missing the (. Thanks guys.

var numbers = 0;
for (numbers = 0; numbers < 101; numbers++) {
  if (numbers % 3 === 0 && numbers != 0)
    if (numbers % 5 === 0)
      document.write("fizzbuzz <br>");
    else {
      document.write("fizz <br>");
    }
  if
  else(numbers % 5 === 0 && numbers != 0) {
    document.write("buzz <br>");
  }
  else {
    document.write("This is the number " + numbers + " in the 10 base number      scale. <br>");
  }
}
3
  • 4
    It's a really good idea to get into the habit of being really consistent about indentation and use of { ... } blocks after for and if blocks. Otherwise, it's easy to get confused about which else go with which if. Commented Jan 20, 2017 at 21:37
  • I went back and added the indentations where it was needed and it looks so much more legible. It definitely makes correcting errors easier. Thanks guys. Commented Jan 20, 2017 at 21:52
  • If an answer helped, please mark it as correct so others who see will know what worked for you ;) Commented Jan 20, 2017 at 22:00

3 Answers 3

3

if else is what you have.

else if is what it should be.

So because you put the if before else, it's expecting a (. This should fix your problem.

Sign up to request clarification or add additional context in comments.

Comments

0

Even though its perfectly fine to use if-else without {} for single line statements, it usually creates problem than saving some characters. Its always preferred to use the {} to avoid confusion even though for single line statements. Also you have interchanged else if with an if else before the check for number%5. You can change your code to below

var numbers = 0;

for(numbers = 0; numbers < 101; numbers++){
  if(numbers%3===0 && numbers !=0) {
    if(numbers%5===0) {
      document.write("fizzbuzz <br>");
    } else{
      document.write("fizz <br>");
    } 
  } else if (numbers%5===0 && numbers !=0){
    document.write("buzz <br>");
  } else{
    document.write("This is the number " + numbers + " in the 10 base number      scale. <br>");
  }
}

Comments

0

The if is written before else when it should be the opposite, see the "<-------------- HERE"-mark.

var numbers = 0;
for (numbers = 0; numbers < 101; numbers++) {
  if (numbers % 3 === 0 && numbers != 0)
    if (numbers % 5 === 0)
      document.write("fizzbuzz <br>");
    else {
      document.write("fizz <br>");
    }
  if <-------------- HERE
  else(numbers % 5 === 0 && numbers != 0) {
    document.write("buzz <br>");
  }
  else {
    document.write("This is the number " + numbers + " in the 10 base number      scale. <br>");
  }
}

It should be:

else if (numbers % 5 === 0 && numbers != 0)
{
    document.write("buzz <br>");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.