JavaScript Algorithm: FizzBuzz javascript fizzbuzz问题

发布时间 2023-06-26 14:38:47作者: Oops!#

We are going to write a function called fizzbuzz that will accept no arguments.

The goal of this function is to print out all numbers from 1 to 100 but with three exceptions:

  1. For every number that is divisible by 3 and 5, console log "FizzBuzz".
  2. For every number that is divisible by only 3 and not 5, console log "Fizz".
  3. For every number that is divisible by only 5 and not 3, console .log "Buzz".
 

Here is a partial example with numbers from 12 to 20:

...
"Fizz"
13
14
"FizzBuzz"
16
17
"Fizz"
19
"Buzz"
...
 

First, we create a for-loop that will count from 1 to 100.

for (let i = 1; i <= 100; i++) {}

Inside the for-loop, we will create a series of if...else statements. The first if-statement checks to see if the number is divisible by both 3 and 5. If that’s the case, we print "FizzBuzz" using console.log().

if (i % 3 === 0 && i % 5 === 0) {
console.log("FizzBuzz");
}

Next, we create an else if statement. If the number doesn’t divide by both 3 and 5 but it is divisible by 3 only, we print out "Fizz".

if (i % 3 === 0 && i % 5 === 0) {
console.log("FizzBuzz");
} else if (i % 3 === 0) {
console.log("Fizz");
}

If a number doesn’t equally divide by both 3 and 5 and it doesn’t equally divide by only 3, then we check to see if the number is divisible by 5 only. If that is the case, we print out "Buzz".

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");
}

Finally, if the number doesn’t divide equally by 3 and/or 5, print out the number itself.

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

That concludes our fizzbuzz function. This algorithm happens to be a common interview question. Employers use this to weed out a lot of programmer candidates. You’ll see all sorts of creative ways to solve this problem especially the ones that have an emphasis on succinctness. My solution isn’t about that. This is the most basic and generic example of FizzBuzz you’ll see and plus, it’s easier to read.

Here is the rest of the code:

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

  

while loop method 

ar output = [];
var count = 1;


function fizzbuzz() {
    // write code here - 1-100 numbers divisible by 3 named fizz and by 5 name buzz, and by both then name fizzbuzz.
    // type fizzbuzz();  in console to make work

    while(count <= 100) {
        

if (count % 3 ==0 && count % 5 ===0) {
    output.push("FizzBuzz");
}

else if (count % 3 ===0) {
    output.push("Fizz");
} 
else if (count % 5 ===0) {
    output.push("Buzz");
}

else {
    output.push(count);  
}

count++;
            }
    
console.log(output);

}