mercredi 16 décembre 2020

can I use the 'switch' statement in any of the questions below? or is the 'else if' the right way to go?

I am trying to write a function that satisfies the following:

-count from 1 to 100, -on numbers divisible with 4 print “byfour”, -on numbers divisible with 6 print “bysix”, -on numbers divisible with both 4 and 6 print “byfoursix”, -skip numbers divisible with 7, -on the number 32 add '!'. This is what I have, but I was wondering if there is way to use the switch statement, or any more optimal way to write it.

function maths(){
  for (let i=1; i<=100; i++){
    if (i === 32){  
    console.log (`${i}!`);
    }
    else if (i % 4 === 0 && i % 6 === 0){
       console.log ("byfoursix");
    }
    else if (i % 4 ===0) {
       console.log ("byfour");
    }
    else if (i % 6 === 0) {
       console.log ("bysix");
    } 
    else if (i % 7 === 0){
       continue;
        }
    else {
      console.log (i);
    }
  }
}

maths();

Any input or advice is super appreciated! Thank you

Aucun commentaire:

Enregistrer un commentaire