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