如何创建功能fizzbuzz [英] How do I create a function fizzbuzz
本文介绍了如何创建功能fizzbuzz的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
创建一个函数fizzBuzz来返回'Fizz','Buzz','FizzBuzz'或它收到的参数,所有这些都取决于函数的参数,可以被3,5或两者整除的数字3分别为。
当数字不能被3或5整除时,数字本身应该被退回
什么我试过了:
编码,但我遗漏了一些我无法弄清楚的东西。
解决方案
通过使用模除法操作,您可以确定一个整数是否可被另一个整数整除而没有余数(可被整除):
Modulo操作 - 维基百科,免费的百科全书 [ ^ ]。
在Python中,与许多其他语言一样,这是通过运算符%完成的:
5。表达式 - Python 2.7.11文档 [ ^ ]。
这就是你所需要的。
-SA
for(var i = 1; i< = 20; i ++){
if(i%3 === 0 && i%5 == = 0){
console.log(FizzBuzz);
}否则if(i%3 === 0){
console.log(Fizz);
}否则if(i%5 === 0){
console.log(Buzz);
}否则{
console.log(i);
}
}
Create a function fizzBuzz to return 'Fizz', 'Buzz', 'FizzBuzz', or the argument it receives, all depending on the argument of the function, a number that is divisible by, 3, 5, or both 3 and 5, respectively.
When the number is not divisible by 3 or 5, the number itself should be returned
What I have tried:
coding, but i am missing something that I cant figure out what.
解决方案
You can determine if one integer is divisible by another one without a remainder ("is divisible") by using modulo division operation:
Modulo operation — Wikipedia, the free encyclopedia[^].
In Python, as in many other languages, this is done with the operator %:
5. Expressions — Python 2.7.11 documentation[^].
This is all you need.
—SA
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);
}
}
这篇关于如何创建功能fizzbuzz的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文