From 58ffcec09b64eedcd828d233a298636592958e96 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Thu, 24 Sep 2020 12:53:20 +0200 Subject: [PATCH] Optimize fizzbuzz --- Exercise 5/script.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Exercise 5/script.js b/Exercise 5/script.js index 36a15cb..893860d 100644 --- a/Exercise 5/script.js +++ b/Exercise 5/script.js @@ -17,14 +17,10 @@ console.log('PART 3') const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] -/* A function that returns eplekake, eple, kake or n based on what n can be divided by */ -const eplekakeCheck = (n) => { - /* if it's divisible by 15 (which is 3*5), it's also divisible by both 3 and 5 */ - if (i % 15 == 0) return 'eplekake'; - else if (i % 3 == 0) return 'eple'; - else if (i % 5 == 0) return 'kake'; - else return String(n); -} +/* A function that returns eple and/or kake if it's divisible by 3 or 5, but defaults +* to n if it's not (an empty string combined with || will return the latter element) +*/ +const eplekakeCheck = (n) => (n % 3 ? '' : 'eple') + (n % 5 ? '' : 'kake') || n; /* Print the output of eplekakeCheck for each element in numbers */ for (i of numbers) {