I'm trying to solve this issue in javascript. I understand in javascript functions (for the most part) can only return one word. I'm trying to figure out how to return multiply words within a function. If theres another way besides a function that I can solve this i'm open to that too.

function aListOfPalindromeWords (str){


const words = "a aa aaa aah aahed aahing aah saal aalii aaliis aals aam";

const myWords = words.split(" ");
var reversedStr = "";

for (let i = 0; i < myWords.length; i++){
for (let j = myWords[i].length; i >=0; i--){
reversedStr += myWords[i];

>   {
if (reversedStr === myWords[i]);
>     {
return myWords[i];

>   }
>  }
>  }
>  }
>  }
console.log(aListOfPalindromeWords ());
1

There are 1 best solutions below

0
DecPK On BEST ANSWER

You can either return an array or a string which will contain word which are palindrome.

You can do this using split, filter, reverse and join.

const words = "a aa aaa aah aahed aahing aah saal aalii aaliis aals aam";

function aListOfPalindromeWords(words) {
    const result = words.split(" ")
        .filter(word => word === word.split("")
            .reverse()
            .join(""));
  
    // AN ARRAY OF PALINDROME STRING
    return result;
}

console.log(aListOfPalindromeWords(words))

If you want to return string which contains only palindrome words then you can return result.join(" "), which will return string.

const words = "a aa aaa aah aahed aahing aah saal aalii aaliis aals aam";


function aListOfPalindromeWords(words) {
    const result = words.split(" ")
        .filter(word => word === word.split("")
            .reverse()
            .join(""));

    // A STRING WHICH CONTAIN ALL PALINDROME WORDS
    return result.join(" ");
}

console.log(aListOfPalindromeWords(words))