From 90e12a35bc1814e4d20ce14c84ef7405e15b1e04 Mon Sep 17 00:00:00 2001 From: Andinus Date: Sat, 4 Sep 2021 23:06:26 +0530 Subject: JS: Raindrops: Remove repeated math logic - Removes repeated math logic. - Compares against "" instead of using .length. - Uses "number" instead of "num". --- javascript/raindrops/raindrops.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'javascript') diff --git a/javascript/raindrops/raindrops.js b/javascript/raindrops/raindrops.js index f8dff04..54696d3 100644 --- a/javascript/raindrops/raindrops.js +++ b/javascript/raindrops/raindrops.js @@ -1,11 +1,17 @@ 'use strict'; -export const convert = (num) => { +export const convert = (number) => { let drops = ""; - if (num % 3 == 0) drops += "Pling"; - if (num % 5 == 0) drops += "Plang"; - if (num % 7 == 0) drops += "Plong"; - if (drops.length == 0) drops += num; + [ + {factor: 3, result: 'Pling'}, + {factor: 5, result: 'Plang'}, + {factor: 7, result: 'Plong'} + ].forEach(drop => { + if (number % drop.factor === 0) + drops += drop.result; + }); + + if (drops === "") drops += number; return drops; }; -- cgit 1.4.1-2-gfad0