Skip to content

Commit f6c0394

Browse files
♻️ Refactor the "Ugly Number" solution
1 parent 71f57f9 commit f6c0394

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

263. Ugly Number/index.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
module.exports = function isUgly(num) {
22
if (num < 1) return false;
3-
let receviedNum = num;
43

5-
while (receviedNum >= 2) {
6-
if (receviedNum % 2 === 0) receviedNum /= 2;
7-
else if (receviedNum % 3 === 0) receviedNum /= 3;
8-
else if (receviedNum % 5 === 0) receviedNum /= 5;
9-
else return false;
4+
for (let divisor of [2, 3, 5]) {
5+
while (num % divisor === 0) {
6+
num /= divisor;
7+
}
108
}
119

12-
return true;
10+
return num === 1;
1311
};

0 commit comments

Comments
 (0)