Can't find a problem 🤨

Can't find a problem :face_with_raised_eyebrow:
0

#1

Greeting coders, I was scratching my head with my code until I found where things got messy. I am wondering why after “result[0][1] = 0” is my variable “curr” changing too. I am only changing one variable. Am I missing some information on arrays or…?

var curr = [
           ['PENNY', 0.01], 
           ['NICKEL', 0.05], 
           ['DIME', 0.1], 
           ['QUARTER', 0.25],
           ['ONE', 1],
           ['FIVE', 5],
           ['TEN', 10],
           ['TWENTY', 20],
           ['ONE HUNDRED', 100]];
  
var result = [];

  var a = curr[0];
  result.push(a);
  result[0][1] = 0;
  return  curr[0]; //returns ["Penny",0]

Thanks for helping out!


#2

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


When arrays or objects are assigned to a variable, only a reference is passed to the variable. See below for explanations of what is happening at each stage of your code.
var curr = [
           ['PENNY', 0.01], 
           ['NICKEL', 0.05], 
           ['DIME', 0.1], 
           ['QUARTER', 0.25],
           ['ONE', 1],
           ['FIVE', 5],
           ['TEN', 10],
           ['TWENTY', 20],
           ['ONE HUNDRED', 100]];
  
var result = [];

var a = curr[0]; // Initialize variable a to refer to an array (curr[0] or ['PENNY', 0.01])
console.log(a); // displays [ 'PENNY', 0.01 ] because a refers to curr[0]

result.push(a);
console.log(result); // displays [ [ 'PENNY', 0.01 ] ]
// since a is just a reference to curr[0] it is like curr[0] was pushed into result

result[0][1] = 0; // this is changing the curr[0]'s 2nd element to 0 because result[0] is the value of a which is a reference to curr[0]
console.log(curr[0]); // displays [ 'PENNY', 0 ]

// Notice below how I can use the a variable to modify the first element of curr[0];
a[0] = 'CHICKEN';
console.log(curr[0]); // displays [ 'CHICKEN', 0 ]

#3

@davoska

@RandellDawson beat me to it as I was typing and he achieved a more clear explanation.
:sunglasses:

Arrays are really passed as pointers to an object.
You create an object curr (which is an array) when you declare the curr variable array.
You create an object result (which is an array) when you declare the result variable array.
You create a pointer to your curr object when you create variable a.
a and curr are one in the same, they are pointing to the same object (one being the object, the other being a replica of the object as a pointer, think of this as a reference or a “mask”)

Any changes to a will also change curr.
Any changes to curr will be reflecting in a when you view a value or property (its pointing to curr).

So when you put a into result, you are really putting a pointer to curr into result.
So when you reference result and make a change, you are changing curr directly, and when you call the pointer (which is a) those changes will be reflected.

You have created object1 (curr), a pointer to that object (a), and then stored the pointer (a) in another object2 (result). So when you work on result right now you are working on the pointer to curr which is a, which will in fact change curr as if you were calling it directly itself.

In essence curr, a, and result are the same in your code as you have set it up and used it.

Does this help?

-WWC


#4

Thank you for the visual edit and a brief, well-made explanation! I didn’t know that they are only references and not separate things. I’ll keep in mind the edit in the future!


#5

Thank you as well! :sweat_smile:
We learn new things everyday, I didn’t know JS uses variables in this concept as pointers. Your explanation is great, thanks!