Skip to content

Instantly share code, notes, and snippets.

@LaisGalvao
Created February 27, 2024 11:53
Show Gist options
  • Save LaisGalvao/c5e94e36988584b9b59fbb0dac4894b0 to your computer and use it in GitHub Desktop.
Save LaisGalvao/c5e94e36988584b9b59fbb0dac4894b0 to your computer and use it in GitHub Desktop.
FizzBuzz Typescript Sample
'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString: string = '';
let inputLines: string[] = [];
let currentLine: number = 0;
process.stdin.on('data', function(inputStdin: string): void {
inputString += inputStdin;
});
process.stdin.on('end', function(): void {
inputLines = inputString.split('\n');
inputString = '';
main();
});
function readLine(): string {
return inputLines[currentLine++];
}
/*
* Complete the 'fizzBuzz' function below.
*
* The function accepts INTEGER n as parameter.
*/
function fizzBuzz(n: number): void {
let i: number;
for (i=1; i <=n; i++){
if (i % 3 === 0 && i % 5 === 0) {
console.log('FizzBuzz')
}
if (i % 3 === 0 && i % 5 !== 0) {
console.log('Fizz')
}
if (i % 3 !== 0 && i % 5 === 0) {
console.log('Buzz')
}
if (i % 3 !== 0 && i % 5 !== 0) {
console.log(i)
}
}
}
function main() {
const n: number = parseInt(readLine().trim(), 10);
fizzBuzz(n);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment