JSFiddle - React, Tailwind, and code Playground
by Tim Ko
JavaScript
function is_palindrome_it(s) {
for (var i = 0, ln = s.length, j = ln - 1; i < ln; i++, j--) {
if (i >= j) {
break;
} else {
if (s[i] != s[j]) {
return false;
}
}
}
return true;
}
function is_palindrome_rec(s) {
function is_palindrome_rec_helper(i, j) {
if (i >= j) {
return true;
} else {
if (s[i] != s[j]) {
return false;
} else {
return is_palindrome_rec_helper(i+1, j-1);
}
}
}
return is_palindrome_rec_helper(0, s.length-1);
}
if (is_palindrome_it("racecar")) {
console.log("correct!");
} else {
console.log("WRONG");
}
if (!is_palindrome_it("racecars")) {
console.log("correct!");
} else {
console.log("WRONG");
}
if (is_palindrome_rec("racecar")) {
console.log("correct!");
} else {
console.log("WRONG");
}
if (!is_palindrome_rec("racecars")) {
console.log("correct!");
} else {
console.log("WRONG");
}