By Hemanta Sundaray on 2021-07-14
A word or sentence that reads the same backward or forward.
Example: radar, level, etc.
Write a function that accepts a string as an argument and returns true, if the string is a palindrome and returns false, if otherwise.
const palin = str => {
const reversedStr = str.split("").reverse().join("")
return str === reversedStr
}
console.log(palin("radar"))
//true
In the code snippet above, on line 2, we are reversing the string received as an argument.
const palin = str => {
const strArray = str.split("")
return strArray.every((char, i) => {
return char === strArray[strArray.length - i - 1]
})
}
console.log(palin("radar"))
// true