Skip to content

Instantly share code, notes, and snippets.

@caglarorhan
Created February 19, 2018 23:21
Show Gist options
  • Save caglarorhan/052ad19a07a2ccbb816e094f7e0e6ed1 to your computer and use it in GitHub Desktop.
Save caglarorhan/052ad19a07a2ccbb816e094f7e0e6ed1 to your computer and use it in GitHub Desktop.
FizzBuzz with ternary operators
function fizzBuzz(num){
var message="";
for(x=1; x<=num; x++){
// if(x%3==0 && x%5!=0){console.log('Fizz');}
// if(x%5==0 && x%3!=0){console.log('Buzz');}
// if(x%3==0 && x%5==0){console.log('FizzBuzz');}
// if(x%3!=0 && x%5!=0){console.log(x);}
p = (x%3==0);
q = (x%5==0);
console.log(p&&q?"FizzBuzz":((p&&!q)?"Fizz":((!p && q)?"Buzz":x)));
}
}
fizzBuzz(20);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment