-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrobogrammatic.js
More file actions
41 lines (35 loc) · 938 Bytes
/
strobogrammatic.js
File metadata and controls
41 lines (35 loc) · 938 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
31
32
33
34
35
36
37
38
39
40
41
var log = console.log;
/**
* @description https://leetcode.com/problems/strobogrammatic-number/
* @param {string} num
* @return {boolean}
*/
var isStrobogrammatic = function(num) {
var numLen = num.length;
var sameRotated = ["8", "0", "1"];
var opposites = {
"6": "9",
"9": "6"
};
var left = "";
var right = "";
var isStrob = true;
for (var i = 0; i < numLen/2; i++) {
isStrob = false;
left = num[i];
right = num[numLen-1-i];
if (left === right && sameRotated.indexOf(left) > -1) {
isStrob = true;
} else if (opposites[left] === right) {
isStrob = true;
}
if (isStrob === false) break;
}
return isStrob;
};
log(isStrobogrammatic("69"));
log(isStrobogrammatic("25"));
log(isStrobogrammatic("88"));
log(isStrobogrammatic("0880"));
log(isStrobogrammatic("0881"));
log(isStrobogrammatic("962"));