jeudi 5 septembre 2019

Is this the correct solution for this exercise? FizzBuzz style game

I am writing code for the exercise below and would like to check if my solution is correct:

Print the numbers 1 through 50.
If the number is divisible by 7, you must skip the next number.
If the number is divisible by 10 or 15, you must print “Donkey!”.
If the number is not divisible by 2 and the previous number is divisible by 10, you must print “Monkey!”.

It seems to work fine but I want to check there is not a better way.

Thank you

for (let i = 0; i <= 50; i++){
  if (i % 7 === 1) { 
    continue;
  } else if (i % 10 === 0 || i % 15 === 0) {
   console.log("Donkey!"); 
  } else if (i % 2 !== 0 && (i - 1) % 10 === 0) {
      console.log("Monkey!");
  } else { 
  console.log(i)
  }
}

Aucun commentaire:

Enregistrer un commentaire