FizzBuzz

Second task:

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

(This is actually an interview question that has been claimed to weed out a significant percentage of programmer candidates. So if you solved it, your labor market value just went up.)

Part 1 solution

let counter = 0
while (counter <= 99) {
if (counter % 3 == 0) {
console.log('fizz')
} else if (counter % 5 == 0) {
console.log('buzz')
} else {
console.log(counter)
}
counter = counter + 1
}

I had to google to add == 0 at the end of my if statements.

Part 2 solution

let counter = 0
while (counter <= 99) {
if (counter % 3 == 0 && counter % 5 == 0) {
console.log('FizzBuzz')
} else if (counter % 3 == 0) {
console.log('fizz')
} else if (counter % 5 == 0) {
console.log('buzz')
} else {
console.log(counter)
}
counter += 1
}

Again, I had to google to get this to work correctly, my "logical" thought was to put numbers that are divisible by both 3 and 5 in the 3rd position like it was explained in the problem statement. Actually it belongs as first if statement you pass through it.

This is as far as I got without google.

let counter = 0
while (counter <= 99) {
if (counter % 3 == 0) {
console.log('fizz')
} else if (counter % 5 == 0) {
console.log('buzz')
} else if (counter % 3 == 0 && counter % 5 == 0) {
console.log('FizzBuzz')
} else {
console.log(counter)
}
counter += 1
}

Another lesson learned, don't just take a problem statement blindly and assume that the logical order of the computation should happen the way it's written in english.

I'm not as confident in my grasp of this exercise because I had to google to fix parts of my code.