0

I was trying to convert alphabets into ASCII using the following:

var subArr = [["S", "E", "R", "R"], ["C", "V", "M", "M", "N", "!"]];
for (var i in subArr) {
              var newStr = subArr[i].map( function(val) {
                return val.charCodeAt()
              })
                                         }
console.log(newStr)       

But it seemed that only the second subarray was converted while the first one was not even considered. Would anyone please help me understand the issue here?

5 Answers 5

3

This is the problem with scope of your print statement. Note that you are iterating for each sublist and reassigning the map result to the same variable newStr so when you console.log it at the end it only contains the value corresponding to the last sublist!

var subArr = [["S", "E", "R", "R"], ["C", "V", "M", "M", "N", "!"]];

for (var i in subArr) {
    var newStr = subArr[i].map(function(val) {
        return val.charCodeAt();
    });

    console.log(newStr);
}  
Sign up to request clarification or add additional context in comments.

Comments

1

console.log(newStr) is outside the loop. You need to create a list and push new values to it.

like this:

var subArr = [["S", "E", "R", "R"], ["C", "V", "M", "M", "N", "!"]];

var resultArr = [];

for (var i in subArr) {
    resultArr.push(subArr[i].map(function(val) {
        return val.charCodeAt();
    }));
} 

console.log(resultArr);

Comments

0

you re not escaping the first array you just crushing the first array with the second one

var subArr = [
  ["S", "E", "R", "R"],
  ["C", "V", "M", "M", "N", "!"]
];

var newStr = [];
for (var i in subArr) {
  newStr = newStr.concat(subArr[i].map(function(val) {
    return val.charCodeAt()
  }))
}


console.log(newStr);

Comments

0

console.log() should be inside(in scope of) for...in loop. In map() function you are converting the array to the character code and assigning it to the variable newStr. So, when the map is executed for the second array element, newStr contains the code of the second element. Thus, printing only for the second array element of subArr.

var subArr = [["S", "E", "R", "R"], ["C", "V", "M", "M", "N", "!"]];
   for (var i in subArr) {
      var newStr = subArr[i].map( function(val) {
                       return val.charCodeAt()
                   }); 
      console.log(newStr)
  }

Comments

0

Your log statement is outside the loop, which means it'll print the value of newStr once after the loop has finished executing.

In your loop, you are using the same variable newStr to store value in each iteration. This means the second iteration will overwrite the first one and so on.

If your intention is to have ASCII codes for each character in place of the character itself, you should be doing the following

var arrays = [
  ["S", "E", "R", "R"],
  ["C", "V", "M", "M", "N", "!"]
];

let asciiArrays = arrays.map(subArray =>
  subArray.map(val => val.charCodeAt())
);

console.log(asciiArrays);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.