简体   繁体   中英

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. For numbers divisible by 3, print "Fizz" instead of the number, and for numbers divisible by 5 (and not 3), print "Buzz" instead.

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

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.

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

2nd - You have a syntax error in the head section. it should be <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:

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/

You can use a while loop, than use a nested if statements to check the conditions.

 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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