Question
Write a js code that prints to the console the numbers from 1 to 100. But for multiples of 3 print “Fizz” instead of the number and for the multiples of 5 print “Buzz”. For numbers which are multiples of both 3 and 5 print “FizzBuzz”?
Answer
*1. We declare a variable*
*2. Do a for loop, let y equal to 1 and whenever y is less than x, the loop runs again*
*3. Check for y values that are divisible by both 3 and 5 and print FizzBuzz instead of the number*
*4. check for y values that are divisible by 3 and print Fizz instead of the number*
*5. check for y values that are divisible by 5 and print Buzz instead*
*6. Else print the number*
let x = 100;
for(let y = 1; y<=x; y++){
if(y%3==0 && y%5==0){ console.log('FizzBuzz') }
else if(y%3==0){ console.log('Fizz') }
else if(y%5==0){ console.log('Buzz') }
else{ console.log(y) }
}
Hope this was helpful
Top comments (1)
This was an interview problem some 20 years ago =)