Paul Davis
Paul Davis

Reputation: 525

Accessing a random variable from an array for use in another varaible

I have one of four possibilities for this particular variable -->

var lowVelocity = Math.random() * (45 - 30) + 30;
var medVelocity = Math.random() * (60 - 45) + 45;
var highVelocity = Math.random() * (80 - 60) + 45;
var hwyVelcoity = Math.random() * (100 - 80) +80;

In one scenario I might have either lowVelocity and medVelocity so I want to choose randomly between those two only. So I did this by:

const velocities = ["lowVelocity", "medVelocity"];
const randomUrban = Math.floor(Math.random() * velocities.length);

And this works - it now chooses one of the two velocities that I am interested in. Low velocity is between 30 and 45 and med is between 45 and 60. In my innerHTML I want to print the number returned from that variable chosen in the array. When the user clicks this starts a function that has this in it.

document.getElementById("scenario").innerHTML = randomUrban; 

But when it prints in the HTML, it doesn't print the number that is associated with the variable, it prints the array number it chose (like 0 or 1).

How do I get the variable (eg if it chose lowVelocity then the number it prints will be the random number lowVelocity found, eg 39) to print instead of the array number?

Upvotes: 2

Views: 126

Answers (4)

secan
secan

Reputation: 2679

Well, you are trying to print the wrong variable; randomUrban is the variable containing the velocities array index selected.

A quick (but pretty dirty) fix would be printing eval(velocities[randomUrban]) but eval() has been deprecated because of security issues.

A bit longer but better solution would be grouping your velocities into a single variable of type object and access its keys to retrieve the numeric value:

const velObj = {
    lowVelocity: Math.random() * (45 - 30) + 30,
    medVelocity: Math.random() * (60 - 45) + 45,
    highVelocity: Math.random() * (80 - 60) + 45,
    hwyVelocity: Math.random() * (100 - 80) + 80
};

const velocities = ["lowVelocity", "medVelocity"];
const randomUrban = Math.floor(Math.random() * velocities.length);
const velKey = velocities[randomUrban];

document.getElementById("scenario").innerHTML = velObj[velKey];
<div id="scenario"></div>

Upvotes: 1

FluffyKitten
FluffyKitten

Reputation: 14312

You could just use a single array to do all of this for you!

If you add the velocities to an array, then the random number can be used as an index into this array. In the code below, we use the length of this array to set the limits for the random number, then you can simply access the value from the velocities array at the position indicated by the random number:

velocities[randomUrban][1]

This gets the sub-array at the position indicated by randomUrban, and [1] gets the second element in the row, i.e. the calculated velocity.

/* Add the velocities to an array or easier management */
var velocities= [
    ["lowVelocity", Math.random() * (45 - 30) + 30],
    ["medVelocity", Math.random() * (60 - 45) + 45],
    ["highVelocity",Math.random() * (80 - 60) + 45],
    ["hwyVelcoity" ,Math.random() * (100 - 80) +80]
]

/* Pick a random position from the velocities */
const randomUrban = Math.floor(Math.random() * velocities.length);

/* Print the velocity at that position. (Also includes the velocity name for illustration) */
document.getElementById("scenario").innerHTML = 
              velocities[randomUrban][0] + " = " + velocities[randomUrban][1];
<div id="scenario"></div>

Note: In your example you only use 2 of the 4 velocities, so you can just add the velocities you want to choose from into the randomVelocities array.

Upvotes: 3

angel.bonev
angel.bonev

Reputation: 2232

In your case just use

document.getElementById("scenario").innerHTML = window[velocities[randomUrban]]; 

var lowVelocity = Math.random() * (45 - 30) + 30;
var medVelocity = Math.random() * (60 - 45) + 45;
var highVelocity = Math.random() * (80 - 60) + 45;
var hwyVelcoity = Math.random() * (100 - 80) + 80;
const velocities = ["lowVelocity", "medVelocity"];
const randomUrban = Math.floor(Math.random() * velocities.length);
console.log(randomUrban);// random from velocities
console.log(velocities[randomUrban]); //get variable name
console.log(window[velocities[randomUrban]]); // search in scope where the variables are declared in this case window

Upvotes: 1

NicklasF
NicklasF

Reputation: 921

How about document.getElementById("scenario").innerHTML = velocities[randomUrban]; ?

Upvotes: 1

Related Questions