Last active
May 23, 2021 11:06
-
-
Save Steakeye/b6448426b3196862b7e083e7293b81f2 to your computer and use it in GitHub Desktop.
NodeJS FizzBuzz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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