2

I'm testing out a rock paper scissor lizard spock game. Instead of generating the possible outcomes, I used var matrix = [ ] to capture all the possibilities. at array location matrix[3,0], the string entered was "rock wins". However, I ran console.log(matrix[3,0]) right after the array and it produced a different result.

can someone tell me why that is?

var matrix = [];

matrix[0,0] = "it's a tie";
matrix[0,1] = "Paper wins";
matrix[0,2] = "rock wins";
matrix[0,3] = "rock wins";
matrix[0,4] = "Spock wins";
matrix[1,0] = "Paper wins";
matrix[1,1] = "it's a tie";
matrix[1,2] = "Scissors win";
matrix[1,3] = "Lizard wins";
matrix[1,4] = "Paper wins";
matrix[2,0] = "rock wins";
matrix[2,1] = "Scissors win";
matrix[2,2] = "it's a tie";
matrix[2,3] = "Scissors win";
matrix[2,4] = "spock wins";
matrix[3,0] = "rock wins";

log.console(matrix[3,0]); //returns spock wins 
1
  • 3
    Hard to comment on code we can't see… matrix[3,0] doesn't do what you think, try matrix[3][0]. Commented Jul 1, 2015 at 5:52

2 Answers 2

4

No, it does not. It actually returns "rock wins".
In short, you use the incorrect syntax.

Why does it happen?

You may miss this part and go right to "How should it be?" if you are not interested in understanding the magic behaviour of JavaScript.

  1. You have declared a 1-dimensional array.
  2. When you try to assign or read the following property matrix[0,0], 0,0 is recognized as an expression which is evaluated to the last number (0 in this case).

It means that

matrix[0,0] = "it's a tie";
matrix[0,1] = "Paper wins";
matrix[0,2] = "rock wins";
// ...
matrix[2,3] = "Scissors win";
matrix[2,4] = "spock wins";
matrix[3,0] = "rock wins";
console.log(matrix[3,0]);

is the same as

matrix[0] = "it's a tie";
matrix[1] = "Paper wins";
matrix[2] = "rock wins";
// ...
matrix[3] = "Scissors win";
matrix[4] = "spock wins";
matrix[0] = "rock wins";
console.log(matrix[0]); // returns "rock wins"

Read more about it in this StackOverflow topic.

The reason of it is that you are using a wrong syntax.

How should it be?

That's how you access value in multidimensional array in JavaScript:

var val = arr[key1][key2][key3]; // not arr[key1, key2, key3]

That's how your code should look like:

var matrix = [];
for (var i = 0; i < 4; i++) matrix[i] = [];

matrix[0][0] = "it's a tie";
matrix[0][1] = "Paper wins";
matrix[0][2] = "rock wins";
// ...
matrix[2][3] = "Scissors win";
matrix[2][4] = "spock wins";
matrix[3][0] = "rock wins";
document.write(matrix[3][0]);  // not matrix[3,0]

Another good option is to describe your 2D array this way:

var matrix = [
    ["it's a tie", "Paper wins", "rock wins"],
    ["Paper wins", "it's a tie", "Scissors wins"]
    // ...     
];
Sign up to request clarification or add additional context in comments.

Comments

1

In your code:

matrix[0,0] = "it's a tie";

the part in square brackets is an expression that is evaluated and coerced to string. The expression 0,0 returns the value after the last "," and evaluates to 0, 0,1 evaluates to 1, and so on.

So:

var matrix = [];

matrix[0,0] = "it's a tie";
matrix[0,1] = "Paper wins";
matrix[0,2] = "rock wins";
matrix[0,3] = "rock wins";
matrix[0,4] = "Spock wins";
matrix[1,0] = "Paper wins";
matrix[1,1] = "it's a tie";
matrix[1,2] = "Scissors win";
matrix[1,3] = "Lizard wins";
matrix[1,4] = "Paper wins";
matrix[2,0] = "rock wins";
matrix[2,1] = "Scissors win";
matrix[2,2] = "it's a tie";
matrix[2,3] = "Scissors win";
matrix[2,4] = "spock wins";
matrix[3,0] = "rock wins";

Creates the "one dimensional" array of the last value assigned to each index:

["rock wins", "Scissors win", "it's a tie", "Scissors win", "spock wins"]

and

matrix[3,0] 

evaluates to

matrix[0]

hence you get "rock wins".

What you want is:

var matrix = [[],[],[],[]];

matrix[0][0] = "it's a tie";
matrix[0][1] = "Paper wins";
matrix[0][2] = "rock wins";
matrix[0][3] = "rock wins";
matrix[0][4] = "Spock wins";
matrix[1][0] = "Paper wins";
matrix[1][1] = "it's a tie";
matrix[1][2] = "Scissors win";
matrix[1][3] = "Lizard wins";
matrix[1][4] = "Paper wins";
matrix[2][0] = "rock wins";
matrix[2][1] = "Scissors win";
matrix[2][2] = "it's a tie";
matrix[2][3] = "Scissors win";
matrix[2][4] = "spock wins";
matrix[3][0] = "rock wins";

console.log(matrix[3][0]); // rock wins

You could also use an array literal:

var matrix = [
              ["it's a tie","Paper wins","rock wins","rock wins","Spock wins"],
              ["Paper wins","it's a tie","Scissors win","Lizard wins","Paper wins"],
              ["rock wins","Scissors win","it's a tie","Scissors win","spock wins"],
              ["rock wins"]
             ];

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.