-1

这是我的 FizzBu​​zz 尝试

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

Codecademy 认为它是正确的,但我想确保它确实如此。非常感谢您提前。PS我真诚地希望这次我的问题不是太模糊或抽象或离题:)

4

5 回答 5

1

这是正确的,但很冗长。

让我们稍微分解一下这个任务。你有两个因素:它是 3 的倍数,还是 5 的倍数?当然,它可以是两种情况,也可以两者都不是,总共有四种情况。像这样:

       | Not x5 |    x5
-------+--------+---------
Not x3 | number |   Buzz
  x3   |  Fizz  | FizzBuzz

您可以非常简单地在代码中表示这一点:

function getFizzBuzz(n) {
    if(n%5) { // not x5
        if(n%3) // not x3 either
            return n;
        else // x3
            return 'Fizz';
    }
    else {
        if(n%3)
            return 'Buzz';
        else
            return 'FizzBuzz';
    }
}

然后只做你的循环:

for(i=1; i<=20; i++) console.log(getFizzBuzz(i));

如果你真的想要,你可以让你的代码超级紧凑。当然,可读性要差得多!

function getFizzBuzz(n) {
    return n%5?(n%3?n:'Fizz'):(n%3?'Buzz':'FizzBuzz');
}

有很多方法可以完成这项任务。不同之处在于其他程序员稍后阅读您的代码的难易程度。

于 2015-02-13T07:34:15.977 回答
0

我认为它可以有多种解决方案,正如 Niet 指出的那样,问题是让它更具可读性。

这是我的带有工作小提琴的版本

代码:

for (var a = 1; a <= 20; a++) {
    var log = (a % 3 == 0 && a % 5 == 0) ? 'fizzbuzz' : null;
    if (!log) log = a % 3 == 0 ? 'fizz' : null;
    if (!log) log = a % 5 == 0 ? 'buzz' : null;
    if (log) console.log(log);
    else console.log(a);
}
于 2015-02-13T08:08:55.013 回答
0

有很多方法可以做到这一点,但重要的是遵循编码指南并使其更简单和可读。

class fizzbuzz {
	
	constructor(fizz,buzz,length){
		this.fizz = fizz;
		this.buzz = buzz;
		this.length = length;
	}

	output() {
		for(let i = 0; i < this.length; i++){
			console.log(this.getFizzBuzz(i));
		}
	}

	getFizzBuzz(i) {
		if(i % this.fizz == 0 && i % this.buzz == 0 )
			return 'fizzbuzz'
		else 
			return i % this.fizz == 0 ? 'fizz' : 'buzz';
	}
}

new fizzbuzz(3,5,100).output();

于 2017-04-24T16:34:29.830 回答
0

我认为你的解决方案很好。它的可读性也很好——这在编程中很重要。

我建议的唯一事情是从 3 和 5 的倍数检查开始(这样您就可以删除非多重检查),使用严格的相等比较运算符 ( ===),并稍微更改您的代码样式 - 再次,可读性是巨大的。(我建议的一些风格改变可能不被推荐,这取决于你最终为谁工作或与谁一起工作,所以对它们持保留态度。)

以下是代码形式的建议:

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)
};
于 2017-03-25T07:22:28.203 回答
0

你会说这太迂回了吗?我有点喜欢这件事的开始,但可能有更好的方法来做到这一点。

编辑:虽然我的其他解决方案肯定比其他解决方案更冗长,但看​​起来更多 XD。

//declare the three arrays to store variables
var threeArray = [];
var fiveArray = [];
var threeFiveArray = [];

//prep your fizzes 
for (i = 3; i < 100; i+=3){
    threeArray.push(i); 
}   

//prep your buzzes 
for (i = 5; i < 100; i+=5){
    fiveArray.push(i);
}

//iterate through 0-99 to see if any of the numbers are in fizz, buzz, or both.
for (i = 0; i < 100; i++){
  if(threeArray.includes(i) && fiveArray.includes(i)){
    threeFiveArray.push(i + ' fizzbuzz');
  } else if(threeArray.includes(i)){
    threeFiveArray.push(i + ' fizz');
  } else if(fiveArray.includes(i)){
    threeFiveArray.push(i + ' buzz');
  } 
}

//return your results
console.log(threeFiveArray);
于 2019-06-01T03:33:52.563 回答