-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisRepdigit.js
More file actions
30 lines (25 loc) · 757 Bytes
/
isRepdigit.js
File metadata and controls
30 lines (25 loc) · 757 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// A repdigit is a positive number composed out of the same digit.
// Create a function that takes an integer and returns whether it's a repdigit or not.
// Examples:
// isRepdigit(66) ➞ true
// isRepdigit(0) ➞ true
// isRepdigit(-11) ➞ false
// Notes:
// The number 0 should return true (even though it's not a positive number).
// Check the Resources tab for more info on repdigits.
function isRepdigit(num) {
num += "";
for (let i = 0; i < num.length; i++) {
if (num[i] != num[0]) {
return false;
}
}
return true;
}
console.log(isRepdigit(66));
// Solution 2 using Set
// function isRepdigit(num) {
// // console.log(new Set("" + num));
// return new Set("" + num).size === 1;
// }
// console.log(isRepdigit(-66));