Javascript map method for an array of string elements

I'm trying to figure out how to implement the map method (rather than using the for loop) to check the string for palindromes and return boolean values ​​to see if the elements of the associated array elements have changed the same way as the original elements of the array. I can not understand the syntax of the map method. How to make the map function for each element in the source array? What value? Here is my working code that only registers the value undefined:

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

console.log(palindromeChecker("What pop did dad Drink today"));

Here is the link to the fiddle: https://jsfiddle.net/minditorrey/3s6uqxrh/1/

There is one related question here:

Javascript array method callback parameters

but it does not answer my confusion regarding the syntax of the map method when used to execute a function in an array of strings.

+4
6

map "" , 1:

var items = [1,2,3];
items.map(function(item) { 
  return item + 1;
});

// returns [2,3,4]

, , :

var items = ['mum', 'dad', 'brother'];
items.map(function(item) {
  return item.split('').reverse().join('') === item;
});

// returns [true, true, false]

100% - map, , , , filter , , false:

var items = ['mum', 'dad', 'brother'];
items.filter(function(item) {
  return item.split('').reverse().join('') === item;
});

// returns ['mum', dad']

, ; , :

var string = 'I live at home with my Mum, my Dad and my Brother!';
var items = string.toLowerCase().replace(/[^a-z0-9-\s]+/, '').split(' ');
items.filter(function(item) {
  return item.split('').reverse().join('') === item;
});

// returns ['i', 'mum', dad']

, return , , :

function checkPalindromes(string) {
  var items = string.toLowerCase().replace(/[^a-z0-9-\s]+/, '').split(' ');
  items.filter(function(item) {
    return item.split('').reverse().join('') === item;
  });

  return items;
}

, :

checkPalindromes('I live at home with my Mum, my Dad and my Brother!'); // ['i', 'mum', 'dad']
+6

- , ES5. , newArray .

, . . .

var arr = [1,2,3,4];
var newArray = arr.map(function(i) {
  return i * 2;
});
//newArray = [2,4,6,8]
0

javascript ( ) - , . (), , , , , , .

, [1,2,3,4] function(item) { return item + 1 }, [2,3,4,5] . , $.map(), .

, : , $.map(), , ( btw). , , , - bools. , , newArray == myArray , $.map(), $.map(). , $.map(), , , - , newArray myArray.

0

newArray myArray. newArray , .

:

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"));
0

, ( "newArray" "myArray" , , , , "undefined" )..

, , , ENTIRE map, ( ).

"" , , , , , "" "" , .

:)

HTML:

<body>

<p id="bla">
BLA
</p>

<p id="bla2">
BLA2
</p>

</body>

JavaScript:

function palindromeChecker(string) {
  var myString = string.toLowerCase();
  var myArray = myString.split(" ");
  var newArray = myArray.filter(function (item) {
    var reversedItem = item.split('').reverse().join('');
    return  item == reversedItem;
});

document.getElementById("bla").innerHTML = myArray;
document.getElementById("bla2").innerHTML = newArray;

}

palindromeChecker("What pop did dad Drink today");
0

, . , . . . , -, . , palindromeChecker .

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
0

Source: https://habr.com/ru/post/1625337/


All Articles