javascript 使用规则打印 1-20 之间的数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19222880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:48:20  来源:igfitidea点击:

print numbers between 1- 20 using rules

javascript

提问by

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

我正在阅读 codeacademys JavaScript 教程,因为我是新手。本教程要求以下内容:

Print out the numbers from 1 - 20.
The rules:
- For numbers divisible by 3, print out "Fizz".
- For numbers divisible by 5, print out "Buzz".
- For numbers divisible by both 3 and 5, print out "FizzBuzz" in the console.
- Otherwise, just print out the number.

打印出 1 - 20 之间的数字
。规则:
- 对于可被 3 整除的数字,打印出“Fizz”。
- 对于可被 5 整除的数字,打印出“Buzz”。
- 对于可以被 3 和 5 整除的数字,在控制台中打印出“FizzBu​​zz”。
- 否则,只需打印出数字。

Here is my code:

这是我的代码:

for (i=1; i<=20; i++) {

if(i%3==0) {
    console.log("Fizz");
} 
if(i%5==0){
    console.log("Buzz");
}else if (i%5==0 && i%3==0) {
    console.log("fizzBuzz");
} else {
    console.log(i);
}

}

i am getting an error saying that i am printing out the wrong number of items, anyone know why that is?

我收到一条错误消息,说我打印的项目数量有误,有人知道这是为什么吗?

采纳答案by Richard Powell

When the value is divisible by 3 and not by 5, on the first If statement "Fizz" is printed.

当该值可以被 3 整除而不是 5 整除时,在第一个 If 语句上打印“Fizz”。

Then on the second if statement, the last else is hit so the number will also be printed. You will need to change the if(i%5==0) to else if.

然后在第二个 if 语句中,最后一个 else 被命中,因此也将打印数字。您需要将 if(i%5==0) 更改为 else if。

However there will be a problem now when (i%5==0 && i%3==0) as the else if for that will never be hit. You can fix this by putting this as the first comparison and changing the output to FizzBuzz.

然而,现在当 (i%5==0 && i%3==0) 作为 else if 永远不会被命中时会出现问题。您可以通过将其作为第一个比较并将输出更改为 FizzBu​​zz 来解决此问题。

Like this:

像这样:

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);
    }
};

Make sure you understand why this fixes your issue before you move on as you will most likely make the same mistake again.

在继续之前,请确保您了解为什么这可以解决您的问题,因为您很可能会再次犯同样的错误。

Add a comment if you would like me to explain clearer if you are struggling to work out why you have gone wrong.

如果您正在努力弄清楚为什么出错,如果您希望我更清楚地解释,请添加评论。

回答by Ferdinand Torggler

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.logstatement.

必须首先检查 3 和 5,否则其他两个语句已经为真并给出单独的日志。现在您可以在单个console.log语句中打印 FizzBu​​zz 。

   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);
      }

    }

回答by Pooja Jadhav

Here is I got the answer correct.

这是我得到的正确答案。

for(var i=1;i<=20;i++)
{

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

The result will be:

结果将是:

1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz

1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBu​​zz 16 17 Fizz 19 Buzz

回答by Fadi

Hi please check the ¨=¨

嗨,请检查¨=¨

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

    else {
        console.log(i);
    }
}

回答by moaz adel

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

回答by Mangesh Bartakke

var i=1;
while (i<21)
{    
    if(i%3!==0 && i%5!==0)
    {
        console.log(i);
    }
    if(i%3===0 && i%5===0 )
    {
        console.log("FizzBuzz");
    }
    else if(i%3===0)
    {
        console.log("Fizz");
    }
    else if(i%5===0)
    {
        console.log("Buzz");
    }   

    i++;
}

回答by Titus the Programmer

I don't know Javascript, but I know some Python and I've come across the same problem. Maybe this can help. I'm sure someone can just translate this for Javascript.

我不知道 Javascript,但我知道一些 Python 并且我遇到了同样的问题。也许这会有所帮助。我敢肯定有人可以将其翻译为 Javascript。

Here's the original problem:Loop through the numbers between 1 and 20. If a number is divisible by 3, print Hip. If the number is divisible by 7, print “Hooray”.

这是最初的问题:循环遍历 1 到 20 之间的数字。如果一个数字可以被 3 整除,则打印 Hip。如果数字能被 7 整除,打印“万岁”。

Here's the code for python for a similar problem:

这是针对类似问题的python代码:

for numbers in range(1,21):
    if numbers % 3 !=0 and numbers % 7 != 0:
        print(numbers)
    if numbers % 3 == 0:
            print("Hip")
    if numbers % 7 == 0:
            print("Hooray")

Here's the output:

这是输出:

1 2 Hip 4 5 Hip Hooray 8 Hip 10 11 Hip 13 Hooray Hip 16 17 Hip 19 20

1 2 臀部 4 5 臀部 万岁 8 臀部 10 11 臀部 13 万岁 臀部 16 17 臀部 19 20

回答by poke

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 Fizzand also you are actually supposed to print FizzBuzzin a single line, instead of two separate ones.

如您所见,即使在打印时也打印出数字Fizz,而且您实际上应该打印FizzBuzz在一行中,而不是两个单独的行中。

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 Buzzseparately as well and if there's no match for it, it will print out the number. So although you already printed Fizzyou are still going to the last else. So you should combine those two separate if blocks into a single one.

要解决前一个问题,您应该查看 if/else 结构。你有一个单独的 if 一开始只是为了Fizz. 之后,您也将Buzz单独处理,如果没有匹配项,它将打印出数字。因此,尽管您已经打印了,但您Fizz仍然要打印到最后一个else. 因此,您应该将这两个单独的 if 块合并为一个。

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

另一个问题是console.log总是会单独写一行。因此,在您检查所有FizzBuzz条件的情况下,您应该打印FizzBuzz. 您还需要先检查一下,否则Buzz条件将首先命中而不给您打印的机会FizzBuzz

回答by Eric Jablow

Try walking through the logic by hand. If iis 1, then only the last block works. If iis 3, the first block works. If iis 5, the second block works. And if iis 15, the second block works, and the third never gets a chance.

尝试手动遍历逻辑。如果i1,则只有最后一个块有效。如果i3,则第一个块起作用。如果i5,则第二个块起作用。如果i15,则第二个块起作用,而第三个块永远没有机会。

In general, have the most restrictive conditions run before the least restrictive. You should also notice and check carefully when two of your blocks are dissimilar. You have four blocks, and only one is an elseblock.

通常,在限制最少的条件之前运行最严格的条件。当您的两个块不同时,您还应该注意并仔细检查。你有四个块,只有一个是else块。

回答by Dan

You should check if a number is divisible by both 3 and 5 in your first IF, since numbers that are divisible by both would otherwise result in the first and the second IF statement being executed.

你应该在你的第一个 IF 中检查一个数字是否可以被 3 和 5 整除,因为可以被两者整除的数字会导致第一个和第二个 IF 语句被执行。

Furthermore, the rules tell you to write out "FizzBuzz" in case a number is divisible by both, and at the moment you're only printing out "Buzz".

此外,规则告诉您写出“FizzBu​​zz”,以防一个数字可以被两者整除,而此时您只打印出“Buzz”。