summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2021-09-04 23:06:26 +0530
committerAndinus <andinus@nand.sh>2021-09-04 23:06:26 +0530
commit90e12a35bc1814e4d20ce14c84ef7405e15b1e04 (patch)
tree996e54646d5d15da97e34b84ab7d38d0a60ef2bc
parent3e70da35ee173955ed9990e5dfd214eb8f693e61 (diff)
downloadexercism-90e12a35bc1814e4d20ce14c84ef7405e15b1e04.tar.gz
JS: Raindrops: Remove repeated math logic
- Removes repeated math logic.
- Compares against "" instead of using .length.
- Uses "number" instead of "num".
-rw-r--r--javascript/raindrops/raindrops.js16
1 files changed, 11 insertions, 5 deletions
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;
 };