Updated 03/07/2023
This is a Rosetta Code post.
Story
A popular interview question is the FizzBuzz test
, its based on a children’s game where you count from 1 to 100 and for multiples of 3 say ‘Fizz’, multiples of 5 say “Buzz”, if both say ‘FizzBuzz’ else say the number.
For example, a typical round of fizz buzz would start as follows:
1 | 1 |
Task
Write a program that prints the numbers from 1 to n. But for multiples of three print Fizz
instead of the number and for the multiples of five print Buzz
. For numbers which are multiples of both three and five print FizzBuzz
.
Solutions
Iterative
- fizzbuzz_iterative.js
1 | export const getFizzBuzz = (upperBound) => { |
- index.js
1 | import {getFizzBuzz} from "./fizzbuzz_recursive.js" |
Recursive
Recursive solution based on code from Chinmay Deshmukh, his was cooler because it used Typescript.
- fizzbuzz_recursive.js
1 | export const getFizzBuzz = (totalNumber, currentNumber = 1) => { |
- index.js
1 | import {getFizzBuzz} from "./fizzbuzz_recursive.js" |