简体   繁体   English

在 JavaScript 上练习

[英]Exercise on javascript

please I'm stuck in this question below since yesterday.请我从昨天开始就被困在下面的这个问题上。 Below is the question:下面是问题:

Write a program that uses console.log to print all the numbers from 1 to 100, with two exceptions.编写一个程序,使用 console.log 打印从 1 到 100 的所有数字,但有两个例外。 For numbers divisible by 3, print "Fizz" instead of the number, and for numbers divisible by 5 (and not 3), print "Buzz" instead.对于可以被 3 整除的数字,打印“Fizz”而不是数字,对于可以被 5(而不是 3)整除的数字,打印“Buzz”代替。

When you have that working, modify your program to print "FizzBuzz", for numbers that are divisible by both 3 and 5 (and still print "Fizz" or "Buzz" for numbers divisible by only one of those).当你有那个工作时,修改你的程序以打印“FizzBu​​zz”,对于可以被 3 和 5 整除的数字(对于只能被其中一个整除的数字,仍然打印“Fizz”或“Buzz”)。

I only got the first two conditions but not the the third.我只得到了前两个条件,但没有得到第三个条件。 I don't know how to go about it anymore, I've tried many options.我不知道该怎么办了,我已经尝试了很多选择。 Below is my code:下面是我的代码:

<html>
<head/head>
<body>
    <script type="text/javascript">
        for (i = 1; i <= 100; i++)
            if (i % 3 == 0) {
                document.write("Fizz");
                document.write("<br />");
            } else if (i % 5 == 0 && i % 3 != 0) {
            document.write("Buzz");
            document.write("<br />");
            } else if (i % 3 && 5 == 0 && i % 3 != 0 && i % 5 != 0) {
                document.write("FizzBuzz");
                document.write("<br />");
            } else {
                document.write(+i);
                document.write("<br />");
            }
    </script>
</body>
</html>

Check the most specific (FizzBuzz) condition first.首先检查最具体的 (FizzBu​​zz) 条件。

function fizzBuzz() {
    for(var i = 1; i <= 100; 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);
        }
    }
}

here is an updated version of your code, I keep it as you write it with some changes, I made it work without touch it's logic, you can see that the problem was in the first comparison and in the second "if else" (5 will never be equal to 0).这是您的代码的更新版本,我在您编写时保留它并进行了一些更改,我让它在不触及逻辑的情况下工作,您可以看到问题出在第一次比较和第二次“if else”(5永远不会等于0)。 you can optimize the code more than that, good luck.你可以优化代码不止于此,祝你好运。

    <html>
      <head/head>
      <body>
          <script type="text/javascript">
              for (i = 1; i <= 100; i++)
                  if (i % 3 == 0 && i % 5 != 0) {
                      document.write("Fizz");
                      document.write("<br />");
                  } else if (i % 5 == 0 && i % 3 != 0) {
                      document.write("Buzz");
                      document.write("<br />");
                  } else if (i % 3 == 0 && i % 5 == 0) {
                      document.write("FizzBuzz");
                      document.write("<br />");
                  } else {
                      document.write(+i);
                      document.write("<br />");
                  }
          </script>
      </body>
      </html>

Since everyone is contributing, I might as well give you an interesting solution:既然大家都在贡献,我不妨给你一个有趣的解决方案:

var i = 101;
while(i --> 0){                                         // as i goes to 0... wat
    var state = !!(i % 3) << 1 | !!(i % 5),             // compute state?
        output = ["FizzBuzz", "Fizz", "Buzz", i];       // hmm...
    console.log(output[state]);                         // output correct string
}

1st - Instead of document.write use console.log like the question says 1st - 而不是document.write使用console.log就像问题说的

2nd - You have a syntax error in the head section. 2nd - 您在 head 部分有语法错误。 it should be <head></head>应该是<head></head>

3rd - for the 1st part of the question all you need is this:第三 - 对于问题的第一部分,您需要的是:

for (i = 1; i <= 100; i++) {
  // if i is divisible by 3      
  if (i % 3 == 0) {
    console.log("Fizz");    
  }
  // if i is divisible by 5 (no need to check for 3 again)
  else if (i % 5 == 0) {
    console.log("Buzz");    
  } 
  // else
  else {
    console.log(i);
  }
}

4th - For the 2nd part you need to add an extra if on top of what you have already:第 4 - 对于第二部分,如果您已经拥有的话,您需要添加一个额外的:

for (i = 1; i <= 100; i++) {
  // if i is divisible by 3 and 5
  if (i % 3 == 0 && i % 5 == 0) {
    console.log("FizzBuzz");
  }
  // if i is divisible by 3      
  else if (i % 3 == 0) {
    console.log("Fizz");    
  }
  // if i is divisible by 5 (no need to check for 3 again)
  else if (i % 5 == 0) {
    console.log("Buzz");    
  } 
  // else
  else {
    console.log(i);
  }
}

working fiddle: https://jsfiddle.net/tedmanowar/amapqcLL/工作小提琴: https : //jsfiddle.net/tedmanowar/amapqcLL/

You can use a while loop, than use a nested if statements to check the conditions.您可以使用 while 循环,而不是使用嵌套的 if 语句来检查条件。

 let number = 0; while (number <= 100) { if(number % 3 === 0 && number % 5 === 0){ console.log("FizzBuzz"); }else if(number % 3 === 0){ console.log("Fizz"); }else if(number % 5 === 0){ console.log("Buzz"); }else{ console.log(number); } number++; }

This solution is the easiest and simplest.这个解决方案是最简单和最简单的。 There are multiple ways to solve the question though.虽然有多种方法可以解决这个问题。

                for (n=1; n<=100; n++){
                   let output = "";
                   if(n % 3=== 0) output += "Fizz"
                   if(n % 5=== 0) output += "buzz"
                   console.log(output || n);
                   }

I don't recommend this answer - since it is very hard to maintain - but it does do it in very few lines.我不推荐这个答案 - 因为它很难维护 - 但它确实在很少的行中完成。 It also relies on the two numbers only having a common factor of 1.它还依赖于只有公因数为 1 的两个数字。

 for(let i=1; i<=100; i++) { document.write(`${i%15?i%5?i%3?i:'Fizz':'Buzz':'FizzBuzz'}<br/>`) }

This is using the ternary operator and backquote template strings.这是使用三元运算符和反引号模板字符串。

You should just use a loop that starts at 1 and is less than 101 (so up to 100) and test for the %n === 0 .您应该只使用一个从 1 开始并且小于 101(最多 100)的循环并测试%n === 0 In other words, make sure there is no remainder.换句话说,确保没有剩余。

 function startConsoleDemo(){ for(var i=1,r; i<101; i++){ // loop from 1 to 100 r = i; // default value of r if(i % 3 === 0 && i % 5 === 0){ // if i/3 and 1/5 do not produce a remainder r = 'FizzBuzz'; // reassign r } else if(i % 3 === 0){ // we knew i % 5 !== 0 so see if i/3 does not produce a remainder r = 'Fizz'; // reassign r } else if(i % 5 === 0){ // we already knew i % 3 !== 0 - you know the drill r = 'Buzz'; // reassign r } console.log(r); // console at each step of the loop no matter what } } startConsoleDemo(); // without () you can use like a var then () later

I have a solution and I'm pretty sure it'll work for you.我有一个解决方案,我很确定它对你有用。

for (let i = 1; i <= 100; i++) {
  if (i % 3 === 0 && i % 5 === 0) {
    document.write(`${i} FizzBuzz`);
  } else if (i % 3 === 0 && i % 5 !== 0) {
    document.write(`${i} Fizz`);
  } else if (i % 5 === 0 && i % 3 !== 0) {
    document.write(`${i} Buzz`);
  } else {
    document.write(`${i}`);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM