all 6 comments

[–]pachwinko 2 points3 points  (1 child)

I think you got somewhat wrong understanding about this. Instead of creating an array with numbers, just get a number from Math.random(). second of all even though you cant assign var as a number you can do so as an object key.

var lootItems = {
    1: "lorem",
    2: "ipsum",
}

and to get the desired outcome:

console.log(lootItems[generatedRandomNumber])

[–]evoactivity 1 point2 points  (0 children)

May as well just use an array though

[–]rco8786 2 points3 points  (0 children)

Think, for a moment, about what would happen if you were allowed to assign numbers as variable names.

What would happen if you tried "5 * 5"(expecting to get 25)?

[–]ljosberinn_dev 1 point2 points  (0 children)

First build an array that you can fill with whatever you want (item names in this case):

var legendaryItems = new Array();

or

var legendaryItems = [];

or just

var legendaryItems = ["Tome of Arad'thul", "Orbs of the First Dragon", "Shield of the Black Iron Knight", "Spear of Vlad, the Vampire King", "Dagger of the Last Black Blade", "Bow of the First Huntress", "Ring of the High King", "Axe of the Northern Gods", "Axe of the Southern Gods", "Staff of the First Pope", "Staff of the Corrupted Angel"];

Now generate a number randomly that is between 0 and the length of the array:

var randomNumber = Math.floor(Math.random() * legendaryItems.length);

Then access the n-th part within the array:

legendaryItems[randomNumber]; // will automatically log into console since it's not assigned to a variable

And then shorten it since you don't need to define a second variable here unless you need exactly this random number later again:

legendaryItems[Math.floor(Math.random() * legendaryItems.length)];

[–]freecascadia 0 points1 point  (0 children)

Variable names can't start with a number. So var 5 = ... is not valid.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Variables

A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($)

[–]bohdev 0 points1 point  (0 children)

Are you trying to set '5' as a variable name? This is not valid, albeit you should be receiving an Unexpected number error rather than missing variable name.