while loop palindrome javascript code example
Example 1: palindrome js
const palindrome = (str) => {
var text = str.replace(/[.,'!?\- \"]/g, "")
return text.search(
new RegExp(
text
.split("")
.reverse()
.join(""),
"i"
)
) !== -1;
}
function palindrome(str) {
let text = str.replace(/[.,'!?\- \"]/g, "").toUpperCase();
for (let i = 0; i > text.length/2; i++) {
if (text.charAt(i) !== text.charAt(text.length - 1 - i)) {
return false;
}
}
return true;
}
console.log(palindrome("Able was I, ere I saw elba."));
Example 2: palindrome javascript stack
function isPalindrome(word) {
let s = new Stack();
for (let i = 0; i < word.length; i++) {
s.push(word[i]);
}
let rword = "";
while (s.length()>0){
rword += s.pop();
}
if (word ==rword){
return true
} else {
return false
}
}
Example 3: javascript palindrome
function remove_spaces_strict(string)
{
return string.replace(/\s/gm, "");
}
function to_lower_case(string="")
{
return string.toLocaleLowerCase();
}
function isPalindrome(string) {
let string1 = string;
if (typeof string === 'string' || typeof string === 'number') {
if (typeof string === 'number') {
string1 = new Number(string1).toString();
}
string1 = remove_spaces_strict(string1);
string1 = to_lower_case(string1);
let len1 = string1.length;
let len = Math.floor(string1.length / 2);
let i = 0;
while(i < len) {
if (string1[i] !== string1[len1 - (1 + i)]) {
return false;
}
console.log(string1[i] + " = " + string1[len1 - (1 + i)]);
i++;
}
}else{
throw TypeError(`Was expecting an argument of type string1 or number, recieved an argument of type ${ typeof string1}`);
}
return string;
}