Skip to content

Instantly share code, notes, and snippets.

@Steakeye
Last active May 23, 2021 11:06
Show Gist options
  • Save Steakeye/b6448426b3196862b7e083e7293b81f2 to your computer and use it in GitHub Desktop.
Save Steakeye/b6448426b3196862b7e083e7293b81f2 to your computer and use it in GitHub Desktop.
NodeJS FizzBuzz
'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'fizzBuzz' function below.
*
* The function accepts INTEGER n as parameter.
*/
function fizzBuzz(n) {
// Write your code here
let i = 0;
while (n > i++) {
let output;
if (i % 3 === 0) {
output = 'Fizz'
}
if (i % 5 === 0) {
output = output ? output + 'Buzz': 'Buzz';
}
console.log(output || i);
}
}
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