Help me understanding parseInt Logic

Tell us what’s happening:
Hi! I don’t understand why the variable binaries only changes in the case number 2, because I lost so much time trying to make the code of case 1 work. Apparently it only needed to be assinged to another variable (result), but I don’t understand why is this.

Thanks!

Your code so far


function binaryAgent(str) {
let binaries = str.split(" ");
binaries.map(x => parseInt(x, 2)); // Case 1: It does NOT change the variable binaries
let result = binaries.map(x => parseInt(x, 2));  // Case 2: this changes the variable binaries
}


binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36 Edg/88.0.705.56.

Challenge: Binary Agents

Link to the challenge:

The Array.map() method doesn’t change the array it is iterating over, it returns a new array with the changes it makes. The first time you call it, other than use up some CPU cycles, it does nothing because you aren’t saving the array it creates. The second time you call it you save the new array to result. But in either case, the array binaries does not change.

4 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.