-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathpalinPerm.js
52 lines (48 loc) · 1.2 KB
/
palinPerm.js
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
42
43
44
45
46
47
48
49
50
51
var palinPerm = function(string) {
// create object literal to store charcount
var chars = {};
var currChar;
var mulligan = false;
var isPerm = true;
// pump characters in, spaces not counted, all lowercase
string.split('').forEach((char) => {
if (char !== ' ') {
currChar = char.toLowerCase();
if (chars[currChar] === undefined) {
chars[currChar] = 0;
}
chars[currChar]++;
}
});
// check that all chars are even count, except for one exception
Object.keys(chars).forEach((char) => {
if (chars[char] % 2 > 0) {
// if more than one exception, return false
if (mulligan) {
isPerm = false; // return in a forEach statment doesn't flow out of function scope
} else {
mulligan = true;
}
}
});
// if not return true
return isPerm;
};
// TESTS
console.log(palinPerm('Tact Coa'), 'true');
console.log(palinPerm('Tact boa'), 'false');
//Another solution
function palindrom(string) {
var test = [];
var arr = string.split('');
for (char of arr) {
if (char !== ' ') {
test.push(char.toLowerCase());
}
}
if (test.join('') === test.reverse().join('')) {
return true;
} else {
return false;
}
}