使用for循环的Fizzbuzz游戏

Fizzbuzz game with for loop

本文关键字:Fizzbuzz 游戏 循环 for 使用      更新时间:2023-09-26

我正在尝试做一个函数,在我的console.log中打印1-27之间的数字。

当一个数字可以被3整除时,它应该将该数字替换为"Fizz"
当一个数字可以被5整除时,将其替换为"嗡嗡"
如果数字可以同时除以3和5,则将其替换为"Fizzbuzz"

参考:http://en.wikipedia.org/wiki/Fizz_buzz)

这是我的代码:

 var fizzbuzz = function(start,stop) {
    for (var x=1;x <= stop; x++)
        var string =',';
    if (x%3 == 0) {
            string += 'Fizz';
    }
    if (x%5 ==  0){
        string += 'Buzz';
    }
    if (x%5 && x%3){
        string += 'Fizzbuzz';
    }
    return string;
};

Console.log给了我",",我不确定我做错了什么。

只是澄清一下。我希望我的答案打印出1,2,Fizz,4,Buzz,Fizz,7,8,Fizz,Buzz,11,Fizz,13,14,Fizz Buzz,16,17,Fizz,19,Buzz,Fizz,22,23,Fizz,Buzz,26,Fizz,依此类推,具体取决于If语句中的"stop"。

Valentins注释是正确的,您确实需要在循环周围添加括号。然而,在循环的每次迭代中,您也要重新定义字符串var。

最后一个if也使输出有点错误,例如15将命中所有3个语句并打印FizzBuzzFizz

所以用这样的东西

var fizzbuzz = function(start,stop) {
  var string = '';
  var addComma = false;
  for (var x=1;x <= stop; x++){
    addComma = false;
    if (x%3 == 0) {
        string += 'Fizz';
        addComma = true;
    }
    if (x%5 ==  0){
        string += 'Buzz';
        addComma = true;
    }
    if(addComma && x!== stop){
        string+=','
    }
  }
  return string;
};

这不是跟踪在哪里添加逗号的最佳方法,但它确实起到了作用。

for(let x = 0; x <=30; x++) {
if (x % 15 === 0) {
    console.log('Fizzbuzz')
} else if (x % 5 === 0) {
    console.log('Buzz')
} else if (x % 3 === 0) {
    console.log('Fizz')
} else {
    console.log(x)
}

}

这就是我处理它的方式

您需要更正for循环

你有

for (var x=1;x <= stop; x++)
    var string =',';

一直执行到CCD_ 2。

如果你想执行这样的单行语句,Javascript允许你避免使用括号

if (a===true)
    alert(a); // This is executed when a === true
alert(b); // This is always executed no matter what a is

这里的缩进是为了表明一点,但if语句会执行所有内容,直到第一个分号。

另一方面,如果你想执行多行代码,如果a === true,你会选择使用大括号,比如

// Alert a and alert b are only executed if a is true
if (a===true) {
    alert(a);
    alert(b);
} 

if语句将执行大括号中的所有内容。

需要注意的是,return停止执行并退出函数。一旦到达return语句,循环就会退出。这就是为什么您应该在for循环之后返回整个字符串。

这是一个更好的实现,但您绝对应该尝试自己实现它

var fizzbuzz = function(start,stop) {
    var string = '';
    for (var x=1;x <= stop; x++) {
        var status = x.toString(); //Each time the loop executes a new variable `status`is created and set to the value `x` for that loop.
        // x is checked as to whether it is divisible by 3 or 5 or both, if it is divisible its status is set to a that value
        if (x%3 === 0) {
            status = 'Fizz';
        }
        if (x%5 ===  0){
            status = 'Buzz';
        }
        if (x%5 === 0 && x%3 === 0){
            status = 'Fizzbuzz';
        }
        string += status; // Append status to the end
        if (x !== stop){ // If x is not equal to the value of stop add a comma
            string += ',';
        }
    }
    return string; //This returns the string value which has had statuses and commas appended to it.
};

这有多个问题:(1) 构建

for (var x=1;x <= 10; x++)
    statement;
otherstatement;

将在执行其他语句之前执行语句10次。在没有大括号的情况下,Javascript假设下一条语句是for循环的内容;

(2) 字符串变量在每个循环中都被重新定义,这个循环去掉了前面的版本,所以return语句只打印出字符串的最后一个值。

(3) 失败的逻辑Buzz if语句是错误的。如果对一个除以15的语句执行此操作,它将执行所有三个语句。因此,第三个iff语句是完全多余的。

解决方案如下:

var fizzBuzz = function(x){
    if(x%15==0){
         return "Fizzbuzz";
    }
    if(x%3==0){
          return "Fizz";
    }
    if(x%5==0){
          return "Buzz";
    }
    return x;
    };
var mainFunction = function(start,stop){
    var str="";
    for(var i=start; i < stop; i++){
          str += fizzBuzz(i) + ", ";
    }
       return str;
    };

请注意,只有当此版本要求打印Fizzbuzz而不是Fizzbuzz时,%15的第三个if语句才是必要的。