1

I'm learning from the book JavaScript for Dummies, and from the following code, it says

console.log( bestAlbumsByGenre[0][1] ) //will output: Patsy Cline:Sentimentally Yours

var bestAlbumsByGenre = []
bestAlbumsByGenre[0] = “Country”;
bestAlbumsByGenre[0][0] = “Johnny Cash: Live at Folsom Prison”
bestAlbumsByGenre[0][1] = “Patsy Cline: Sentimentally Yours”;
bestAlbumsByGenre[0][2] = “Hank Williams: I’ m Blue Inside”;

but in the console the output is: "o". Why is that, and what am I doing wrong?

10
  • 1
    That code does not make any sense. [0][0] would be a string and it is acting like it is an array. The line should be bestAlbumsByGenre[0] = []; Commented Sep 18, 2018 at 14:36
  • It really doesn't, you are setting a character of a String to a String, where is that in Js for Dummies? Commented Sep 18, 2018 at 14:37
  • I've added your code to a snippet. Click Run code snippet and you'll see the errors. Commented Sep 18, 2018 at 14:37
  • 1
    If you replaced the = "Country"; in the second line with = [];, it will do what you likely want. This at least is an actual multidimensional array. Commented Sep 18, 2018 at 14:42
  • 1
    Possible duplicate of Declare an empty two-dimensional array in Javascript? Commented Sep 18, 2018 at 15:34

3 Answers 3

2

You seem to have mixed up two different exercises. The following line is resulting in the error:

bestAlbumsByGenre[0] = "Country";

I've cleaned up the code to make it work.

However, I think I would prefer an object, where each key represents the genre, and their value is an array.

// Define the outer array
const bestAlbumsByGenre = [];

// Set the first element of the array as an array
bestAlbumsByGenre[0] = [];

// Add items to the first element (the array)
bestAlbumsByGenre[0][0] = "Johnny Cash: Live at Folsom Prison"
bestAlbumsByGenre[0][1] = "Patsy Cline: Sentimentally Yours";
bestAlbumsByGenre[0][2] = "Frank Williams: I’ m Blue Inside";

console.log(bestAlbumsByGenre[0][1]);

// Alternative approach
const reallyBestAlbumsByGenre = {
    rock: [],
};

reallyBestAlbumsByGenre.rock.push("Johnny Cash: Live at Folsom Prison");
reallyBestAlbumsByGenre.rock.push("Patsy Cline: Sentimentally Yours");
reallyBestAlbumsByGenre.rock.push("Frank Williams: I’ m Blue Inside");

console.log( reallyBestAlbumsByGenre.rock[1] ); 

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, Cheers
This is by far the most thorough example. It shows how to fix the problem for the exercise using a 2D array as well as explains how to do it with an object.
0

Since you want to organize albums by genre, it would make more sense to create an object with the genre as a key:

var bestAlbumsByGenre = {
    "Country": [
        "Johnny Cash: Live at Folsom Prison",
        "Patsy Cline: Sentimentally Yours",
        "Hank Williams: I’m Blue Inside",
    ]
}

3 Comments

This is certainly correct. But the OP mentioned that this was an attempt to understand an example in a beginners' book.
Thanks a lot, hav a gud day.
agree with @ScottSauyet, I'll add that the exercise is about arrays[], not about objects{}
0

Your not actually accessing a two dimensional array, but you are accessing the second character of a string.

Your are initializing a 1 dimensional array of string when you do:

When you did the following:

var bestAlbumsByGenre = [];
bestAlbumsByGenre[0] = "Country";

You assigned a string to the first element.

Subsequently, the other statements did nothing.

Fix

The following fixes your error:"

var bestAlbumsByGenre = [[]]
bestAlbumsByGenre[0][0] = "Country";

6 Comments

yup, so how can i access, lets say: [country] [Johnny Cash..] from the mentioned code
@Mattey var bestAlbumsByGenre = [[]] , bestAlbumsByGenre[0][0] = "Country";
it does thanks, but can i access some inner elements within the array, maybe: bestAlbumsByGenre[0][1] //“Patsy Cline: Sentimentally Yours”; is there a way to do it
@Maltey: did you try it?
Wouldn't it be better in this case to separate the creation of the outer array from the creation of the inner one? It's odd if you then add the "Jazz" category and have to add that specifically to the outer array, but magically the initial "Country" one was there. So I would do var bestAlbumsByGenre = [] followed by bestAlbumsByGenre[0] = []. It certainly seems a better approach for teaching beginners.
|

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.