From f47a55b7c332f91693b027c1ee10ca93d591b474 Mon Sep 17 00:00:00 2001 From: gremble0 Date: Mon, 23 Sep 2024 11:10:05 +0200 Subject: [PATCH] Complete exercise --- src/fizzbuzz.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/fizzbuzz.js b/src/fizzbuzz.js index e3ec3a8..bccabd0 100644 --- a/src/fizzbuzz.js +++ b/src/fizzbuzz.js @@ -1,10 +1,26 @@ -const answer = [] - -// Write your code below this line +/** + * @param {number} num + * @returns {any[]} + */ +function fizzbuzz(num) { + let out = new Array(num) + let iter = '' + for (let i = 1; i <= num; ++i) { + if (i % 3 == 0) iter += 'Fizz' + if (i % 5 == 0) iter += 'Buzz' + if (iter === '') out[i - 1] = i + else { + out[i - 1] = iter + iter = '' + } + } + return out +} +const answer = fizzbuzz(15) // Don't touch the code below this line, we'll cover it later module.exports = answer