Skip to content

Instantly share code, notes, and snippets.

@faddah
Last active July 22, 2020 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save faddah/c7190f96fef19cb6fb19c2e23b61313d to your computer and use it in GitHub Desktop.
Save faddah/c7190f96fef19cb6fb19c2e23b61313d to your computer and use it in GitHub Desktop.
JavaScript FizzBuzz example
'use strict';
/*
* Complete the 'fizzBuzz' function below.
*
* The function accepts INTEGER n as parameter.
*/
function fizzBuzz(n) {
if(typeof n !== 'number') {
console.log("The input must be of Type Number.")
return
}
let result = '';
for(let i = 1; i < n + 1; i++) {
if ((i % 3 == 0) && (i % 5 == 0)) {
result.concat('\n', 'FizzBuzz')
}
else if (i % 5 == 0) {
result.concat('\n', 'Buzz')
}
else if (i % 3 == 0) {
result.concat('\n', 'Fizz');
}
else {
result.concat('\n', i);
}
};
return console.log(result);
}
function main() {
const n = parseInt(readLine().trim(), 10);
fizzBuzz(n);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment