Javascript map method on array of string elements

后端 未结 8 563
悲哀的现实
悲哀的现实 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:08

    Thanks for your input, all. This is the code I ended up with. I fixed the scope issues in the original post. My main problem was understanding the syntax of the map method. In particular, I could not understand from other online resources how to determine the value in the callback function. So, with much help from above I have placed the map method inside the palindromeChecker, and done all of the work on the array inside the map function.

    var palindromeChecker = function(string) {
    var newString = string.toLowerCase().split(' ');
    newString.map(function(item) {
      console.log(item.split('').reverse().join('') === item);
      });
    }; 
    
    palindromeChecker("What pop did dad drink today");
    
    //Returns false, true, true, true, false, false
    

提交回复
热议问题