17

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 array elements reversed are the same as the original array elements. I cannot seem to understand the syntax of the map method. How do I get the map to function on each element in the original array? What is the value? Here is my working code, which is only logging a value of 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 a link to the fiddle: https://jsfiddle.net/minditorrey/3s6uqxrh/1/

There is one related question here:

Javascript array map method callback parameters

but it doesn't answer my confusion about the syntax of the map method when using it to perform a function on an array of strings.

Community
  • 1
  • 1
Mindi Torrey
  • 285
  • 1
  • 6
  • 14
  • 2
    well you are getting `undefined` because you aren't returning anything from `palindromeChecker` - nothing to do with `map()` – Rhumborl Jan 21 '16 at 13:53
  • Are you trying to see if words inside a sentence are palidromes or if the entire sentence is. Seems weird you would use map in the first place. ANd in your fiddle you are trying to log variables in global scope that are not in global scope. – epascarello Jan 21 '16 at 13:59
  • Thanks, epascarelgo. I agree map is not the ideal way to do this, I am trying to figure out how to do it, nonetheless, to better understand how the map method can work with arrays with string elements. Thanks for noting the issue with scope. I'm at a pretty basic level with coding. – Mindi Torrey Jan 21 '16 at 19:07

8 Answers8

17

The map method will literally 'map' a function call onto each element in the array, take this as a simple example of increasing the value of each integer in an array by 1:

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

// returns [2,3,4]

In your case, you are trying to use map to accept or reject a string if it's a palindrome, so a simple implementation might be:

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

// returns [true, true, false]

I'm not 100% sure of your reasons for using map, because if you were trying to just filter the array and remove the strings that aren't palindromes, you should probably use the filter method instead, which works in the same way, but would remove any that return false:

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

// returns ['mum', dad']

In your case you are splitting a string first to get your array of characters; you may also want to make that string lower case and remove punctuation, so an implementation might be:

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']

As mentioned in one of the comments on your question, you need to ensure you return a value from your function if you are using a separate function to perform the check, so this is how your function should look:

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;
}

And you would call it using:

checkPalindromes('I live at home with my Mum, my Dad and my Brother!'); // ['i', 'mum', 'dad']
toomanyredirects
  • 1,972
  • 15
  • 23
  • You are still missing why the method returns undefined. – epascarello Jan 21 '16 at 14:01
  • 2
    Thanks, @toomanyredirects. This was really helpful. I agree the filter function is more practical for most situations, but I am really just trying to learn how to use the map method with arrays with string elements. Really appreciate your careful answer! – Mindi Torrey Jan 21 '16 at 20:05
8

try something like this:

let str = 'hello';
let tab = [...str];

tab.map((x)=> {
    console.log("|"+x+"|");
    return x;
})
marqn
  • 81
  • 1
  • 1
1

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"));
Cem Ekici
  • 364
  • 1
  • 8
  • This returns only a single value comparing the original string to the reversed string. I am trying to return boolean values comparing each element of the array--each word--to its reversed word to see whether each word is a palindrome or not. Thus, returning as many boolean values as words in the string. – Mindi Torrey Jan 21 '16 at 19:04
1

Javascript map method on array of string elements by using split() function.

let str = 'hello';
      
    str.split('').map((x)=> {
        console.log("|"+x+"|");
        return x;
    })
0

Map is a higher-order function available in ES5. I think your newArraywill contain an array of boolean values.

In essence, map will iterate over every value in your array and apply the function. The return value will be the new value in the array. You can also use map and save the information you need somewhere else, and ignore the result of course.

var arr = [1,2,3,4];
var newArray = arr.map(function(i) {
  return i * 2;
});
//newArray = [2,4,6,8]
froginvasion
  • 833
  • 6
  • 19
  • I tried this with string array and I get some issue: `var a = ["a","b","vvv"]; a.map((key)=>{return {key:0}}); ` , I get array where all the keys are *key* – Pini Cheyni Aug 15 '18 at 05:55
0

The map function in javascript (and pretty much in any language) is a great little function that allows you to call a function on each of the items on a list, and thus changing the list itself. The (anonymous) function you're passing as an argument accepts an argument itself, which is filled by an item of the list it is working on, each time it is called.

So for a list [1,2,3,4], the function function(item) { return item + 1 }, would give you a list of [2,3,4,5] for a result. The function you passed to $.map() is run over each element of the list, and thus changing the list.

So for your code: in the function you're passing as an argument to $.map(), you're returning whether the old and new array are equal (which is false btw). So since you're returning a boolean value, the list you'll end up with is a list of bools. What I think you want to do, is extract the newArray == myArray from the function you're passing to $.map(), and putting it after your $.map() call. Then inside the function you're passing to $.map(), return the item you're splitting and whatnot, so your newArray will be an array of strings like myArray.

Glubus
  • 2,819
  • 1
  • 12
  • 26
0

Apart from a few minor mistakes in your code, such as scope issues (you're referencing the "newArray" and "myArray" outside of the function in which they where defined, and therefore, getting "undefined")..

The main issue you had is that you addressed the ENTIRE array inside the map function, while the whole concept is breaking things down to single elements (and then the function collects everything back to an array for you).

I've used the "filter" function in my example, because it works in a similar manner and I felt that it does what you wanted, but you can change the "filter" to a "map" and see what happends.

Cheers :)

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");
DougieHauser
  • 460
  • 6
  • 14
0

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
Mindi Torrey
  • 285
  • 1
  • 6
  • 14