rycankun rycankun - 1 month ago 6
Javascript Question

charCodeAt() inside map function returning original array

I have the following;

function rot13(str) {
var result = str.split("");
result.map(function(val) {
return val.charCodeAt();
});
}
rot13("SERR PBQR PNZC");


when I run this however the returned array is the same as the input array

["S", "E", "R", "R", " ", "P", "B", "Q", "R", " ", "P", "N", "Z", "C"]


Could someone help me understand why this isn't working?

Answer Source

Array.map creates a new array and does not mutate the original array, so you have to return it explicitly or assign it to a variable:

function rot13(str) { 
 var result = str.split("");
 return result.map(function(val) {
   return val.charCodeAt(); 
 });
}