/
Palindrome.js
44 lines (33 loc) · 939 Bytes
/
Palindrome.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
/* Using the JavaScript language, have the function Palindrome(str) take the str parameter being passed
and return the string true if the parameter is a palindrome, (the string is the same forward as it is
backward) otherwise return the string false. For example: "racecar" is also "racecar" backwards. Punctuation
and numbers will not be part of the string.
*/
function Palindrome(str) {
var isPalindrome = false;
var wasFalse = false;
var strArray = str.split('');
for (i = 0; i < strArray.length; i++){
for (j = strArray.length - 1; j > -1; j-- ){
if (strArray[i] == " "){
i++;
} else if(strArray[j] == " ") {
j--;
}
if (strArray[i] == strArray[j]){
isPalindrome = true;
i++;
} else {
wasFalse = true;
}
}
}
if (!wasFalse) {
return isPalindrome;
} else {
return false;
}
}
//Palindrome('never odd or even');
//Palindrome('eye');
Palindrome('Adam Richman');