print numbers between 1- 20 using rules

后端 未结 10 1850
不思量自难忘°
不思量自难忘° 2020-12-11 09:45

I am going through codeacademys JavaScript tutorials as i am new to it. The tutorial asks for the following:

Print out the numbers from 1 - 20.
The rules:

10条回答
  •  爱一瞬间的悲伤
    2020-12-11 10:29

    Did you try it out yourself? Because when you run it you get this output:

    1
    2
    "Fizz"
    3
    4
    "Buzz"
    "Fizz"
    6
    7
    8
    "Fizz"
    9
    "Buzz"
    11
    "Fizz"
    12
    13
    14
    "Fizz"
    "Buzz"
    16
    17
    "Fizz"
    18
    19
    "Buzz"
    

    As you can see, you are printing out the number even when you printed Fizz and also you are actually supposed to print FizzBuzz in a single line, instead of two separate ones.

    To fix the former issue you should take a look at your if/else structure. You have a separate if at the beginning just for Fizz. After that, you are handling the Buzz separately as well and if there’s no match for it, it will print out the number. So although you already printed Fizz you are still going to the last else. So you should combine those two separate if blocks into a single one.

    The other issue is that console.log will always write a separate line. So in your case where you check for all the FizzBuzz conditions, you should print FizzBuzz. You will also want to check that first, otherwise the Buzz condition will hit first without giving you a chance to print FizzBuzz.

提交回复
热议问题