print numbers between 1- 20 using rules

后端 未结 10 1849
不思量自难忘°
不思量自难忘° 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:16

    The check for both 3 and 5 must be first, otherwise the two other statements are true already and give separate logs. Now you print FizzBuzz in a single console.log statement.

       for (i=1; i<=20; i++) {
    
          if (i%5==0 && i%3==0) {
              console.log("FizzBuzz");
          } else if(i%3==0) {
              console.log("Fizz");
          } else if(i%5==0){
              console.log("Buzz");
          } else {
              console.log(i);
          }
    
        }
    

提交回复
热议问题