0

Here's my code:

var album1 = "First Album name";
var album2 = "Second Album Name";
var album3 = "Third Album Name";
var album4 = "Fourth Album name";
var album5 = "Fifth Album name";

var currentAlbumCtr = 1;
var currentAlbum = album + currentAlbumCtr;

currentAlbum should be set to album1 if currentAlbumCtr is 1. Can anyone help?

teo751
  • 321
  • 1
  • 4
  • 13
  • Have a read here... http://www.w3schools.com/js/js_obj_array.asp – Mark Setchell Apr 06 '14 at 22:34
  • 4
    You shall create an array of albums, so that you can get a specific index – xav Apr 06 '14 at 22:34
  • 2
    http://eloquentjavascript.net/chapter4.html – Bergi Apr 06 '14 at 22:34
  • This does indeed look like a good use case for an array. – Paul Apr 06 '14 at 22:36
  • 1
    and most of these: [`[javascript] dynamic variables`](https://stackoverflow.com/search?q=[javascript]+dynamic+variables). – Felix Kling Apr 06 '14 at 22:59
  • @adeneo I think Bergi was right. It is an XY. In a real app, there would be a database of albums. The programmer would query it in code and probably receive back an array of objects. The OP should really be asking "how do I work with album data in a sustainable way?" rather than "how do I access a bunch of globals as if it were an organized array?" – Paul Apr 06 '14 at 23:08

4 Answers4

3

You could use eval to achieve what you want, but that would probably be the wrong way to do it. Why not use an array:

var album = [];
album[1] = "First Album name";
album[2] = "Second Album Name";
album[3] = "Third Album Name";
album[4] = "Fourth Album name";
album[5] = "Fifth Album name";

currentAlbumCtr = 1;
currentAlbum = album[currentAlbumCtr];

Or, alternatively,

var album = {
    "1": "First Album name";
    "2": "Second Album Name";
    "3": "Third Album Name";
    "4": "Fourth Album name";
    "5": "Fifth Album name";
};

Or even

var album = [
    "First Album name",
    "Second Album Name",
    "Third Album Name",
    "Fourth Album name",
    "Fifth Album name"
];

currentAlbumCtr = 1;
currentAlbum = album[currentAlbumCtr-1];

Note the -1 on the last statement since arrays are normally indexed from 0

Stephen Thomas
  • 13,843
  • 2
  • 32
  • 53
  • 2
    Your array indices are odd. Why not use an array literal? – Bergi Apr 06 '14 at 22:36
  • Closest to the OP's original code. There's nothing wrong with skipping an index. – Stephen Thomas Apr 06 '14 at 22:36
  • 1
    …apart from that `album.length` is `6`? – Bergi Apr 06 '14 at 22:37
  • @Bergi Only about half of his array indices are odd. There does seem to be a choice between array index/ description mismatch and proper array length. Usually the first concern loses because the user doesn't need to see the array indexes. – Paul Apr 06 '14 at 22:49
  • @Paul: I didn't mean "odd" as in "not even", but rather "strange". I'm not a native speaker :-/ How would you have phrased that? – Bergi Apr 06 '14 at 22:54
  • @Bergi Ah well, that word "odd" has both meanings so you did OK. You could have said "Why are you creating an array-like object with string indices instead of an array?" but what you said was fine. – Paul Apr 06 '14 at 22:58
  • Thanks - I feel dumb for not originally thinking to use an array... lazy sunday I guess. – teo751 Apr 06 '14 at 23:02
0

currentAlbum should be set to album1 if currentAlbumCtr is 1.

if (currentAlbumCtr == 1) {
    currentAlbum = album1;
}
0

Not exactly sure what you're asking for. Does this work?

script:

var albums =
           [ "album1",
             "album2",
             "album3" ];

$("#clickme").click(function(){
    var currentAlbum = parseInt($("#album_number").val()) % albums.length;
    alert(albums[currentAlbum]);
})

HTML:

<button id="clickme">click!</button>
<input id="album_number"></input>
royhowie
  • 11,075
  • 14
  • 50
  • 67
  • @Bergi Well, that was just for grabbing the input value. I wasn't sure bow the OP wanted to set it up. – royhowie Apr 07 '14 at 01:00
-1
var currentAlbum = eval("album" + currentAlbumCtr);
Taher
  • 11,902
  • 2
  • 28
  • 44
  • That's why `eval` is evil. – Bergi Apr 06 '14 at 22:36
  • eval is not evil in this case, ps: that could do without -1, its a working solution still – Taher Apr 06 '14 at 22:39
  • 2
    No, this is exactly the case where [`eval` is a bad idea](http://stackoverflow.com/a/87260/1048572), and that's worth a downvote. Especially when introducing this to a learner: At some day he will learn what `eval` is good for, but he will need to understand arrays first. – Bergi Apr 06 '14 at 22:41
  • 1
    `currentAlbumCtr = ';document.write("Minimalism forever.")'` – Paul S. Apr 06 '14 at 22:45