this is my algorithm palindrome checker, if you have any algorithm that more than eficient. Please share to me.
function palindrome(str) {
let revers = str.toLowerCase()
let spl = revers.split("");
let fil = spl.filter(e => e !== " " && e !== "." && e !== "," && e !== "_"&& e !== "-" && e !== "/" && e !== ")" && e !== "(");
let asc = fil.join("");
let dsc = fil.reverse().join("");
return asc === dsc
}
console.log(palindrome("My age is 0, 0 si ega ym."));
// 1. string diconvert ke lowercase
// 2. string di split masing masing satu karakter
// 3. filter semua tanda baca yang ada
// 4. buat variable yang berisi hasil filter dan sufah dijoin menjadi string
// 5.buat variable dari hasil filter yang dibalik dengan memggunakan prototype array reverse() lalu di join
// 6. return dengan compare antara dua variable terakhir. jika sama hasilnya true jika berbeda hasilnya false
I used a regex. so it reults in just three one line functions.
The bottom two lines are how I run the tests.
I’ve blurred it as the mods are asking us not to post solutions unless we blur them
let txtClean = str => str.replace(/(\W|_)/g, '').toLowerCase();
let txtRev = str => str.split('').reverse().join('');
let palindrome = str => txtClean(str) === txtRev(txtClean(str)) ? true: false;
const testArray = ["eye", "_eye", "race car", "not a palindrome", "A man, a plan, a canal. Panama", "never odd or even", "nope","almostomla","My age is 0, 0 si ega ym.", "1 eye for of 1 eye.","0_0 (: /-\ :) 0-0","five|\_/|four"]
testArray.forEach(test => console.log(palindrome(test)));
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.