B
B
Byz2020-11-15 13:19:09
JavaScript
Byz, 2020-11-15 13:19:09

Problem with array code from JavaScript for Dummies, what could be the problem?

The problem is that when writing this code, 5fb0fc929ad5e743364932.pngAND it is that when trying to get the answer in two different ways, it should return "PatsyCline:Sentimentally Yours" , but returns only the letter of the element, depending on the element index. Example in the second picture 5fb100204e9ac942581335.png
And if you do it through a web page, the situation is the same.
Could this be due to the fact that the code in the book is written in ES5?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hellcaster, 2020-11-15
@lefshik

I can't even imagine what kind of book this is...
You create an array var bestAlbumsByGenre = []
Next, you assign the first element to a STRING bestAlbumsByGenre [0] = "Country"
And then you assign a new value to the string at index 0 bestAlbumsByGenre [0][0] = "JohnyCash"
The result of bestAlbumsByGenre[0][0] will be the letter C (the zero element of the zero element of the bestAlbumsByGenre array, that is, the first letter of the string Country), and you assign another string instead - this is not accepted with us, this does not work
If you go for it to work, then it must be either an object

var bestAlbumsByGenre  = {
    "Country": ["JohnyCash", "PatsyChine"],
    "Rock": []
}

or an array instead of a string
bestAlbumsByGenre [0] = []
bestAlbumsByGenre [0][0] = "JohnyCash"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question