Javascript map method on array of string elements

后端 未结 8 468
悲哀的现实
悲哀的现实 2021-01-11 10:09

I am trying to understand how to implement the map method (rather than using a for loop) to check a string for palindromes and return boolean values for whether the mapped a

8条回答
  •  情话喂你
    2021-01-11 11:01

    newArray should include reversed version of theall items in myArray. After that, newArray should be reversed and joined with space in order to get the reversed version of the input string.

    Here is the code:

    function palindromeChecker(string) {
      var myString = string.toLowerCase();
      var myArray = myString.split(" ");
      var newArray = myArray.map(function (item) {
            return item.split("").reverse().join("");
        });
      console.log(newArray);
      return newArray.reverse().join(" ") === string;
    }
    
    console.log(palindromeChecker("dad did what"));
    

提交回复
热议问题