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?

KJH
  • 2,382
  • 16
  • 26
Mattey
  • 69
  • 7

3 Answers3

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] ); 
dschu
  • 4,992
  • 5
  • 31
  • 48
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";
serge
  • 13,940
  • 35
  • 121
  • 205
Menelaos
  • 23,508
  • 18
  • 90
  • 155
  • yup, so how can i access, lets say: [country] [Johnny Cash..] from the mentioned code – Mattey Sep 18 '18 at 14:44
  • @Mattey `var bestAlbumsByGenre = [[]]` , `bestAlbumsByGenre[0][0] = "Country";` – Menelaos Sep 18 '18 at 14:45
  • 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 – Mattey Sep 18 '18 at 14:49
  • @Maltey: did you try it? – Scott Sauyet Sep 18 '18 at 14:50
  • 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. – Scott Sauyet Sep 18 '18 at 14:56
  • @ScottSauyet, it does work, thanks a lot. think I need to keep on learning about arrays and Objects. – Mattey Sep 18 '18 at 15:18
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",
    ]
}
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268