mercredi 2 septembre 2020

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?




Aucun commentaire:

Enregistrer un commentaire