Home > Blockchain >  newbie on javascript ( while loop )
newbie on javascript ( while loop )

Time:03-06

hi everyone am fairly new to javascript , so i wrote this fizzbuzz challenge but somehow it wouldn't run i don't know what to do , thanks and here's the code :

var output = []
var count =1;

function fizzBuzz(){
  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);
}

CodePudding user response:

i would have used a for loop there, even tho that would work fine, you didnt call the function. add fizzBuzz(); at the bottom

CodePudding user response:

var output = [];

function fizzBuzz(){
  for(var i=0; i<100; i  ){
    if (i%3===0 && i%5===0){
     output.push("fizzbuzz");
    }else if (i%3===0){
      output.push("fizz");
    }else if (i%5===0){
      output.push("buzz");
    }else {
      output.push(i);
    }
    console.log(output);
  }
}

fizzBuzz();

for the while loop

that way you dont need a count variable and its much cleaner

  • Related