Search This Blog

2023/09/11

Javascript Interview Question:List palindrome words from word's array

List the words from array which are palindrome.

Code:
let words= ["Radar", "hello", "level", "world", "deified"]
let palindromeWords= [];

for (let word of words) {
//reverse a word
var reversedWord = word.split('').reverse().join('')

if (word.toUpperCase() == reversedWord.toUpperCase()) {
palindromeWords.push(word)
}
}

console.log(palindromeWords)

Output:
[ 'Radar', 'level', 'deified' ]

No comments:

Post a Comment