It does not work this is for an API key.
let hum = 'ethan';
let groceryList = ['bread', 'tomatoes', 'milk'];
let random = Math.floor(Math.random() * 3);
let em = '';
if (random === 0) {
groceryList[0] === em;
}
if (random === 1) {
groceryList[1] === em;
};
if (random === 2) {
groceryList[2] === em;
};
if (random === 3) {
hum === em;
}
console.log(em);
3
Answers
Assignment uses a single equals sign i.e.
=
.Not sure what you are trying to achieve here, but you can reduce your multi-if block into a simple if-else.
Use the grocery list length as a way to generate a random value.
In JavaScript,
===
is a comparison operator that tests the equality of two values without performing any type conversion.So consider whether you really need to use the
===
operator in the condition.And when you do
groceryList[1] === em;
You are actually checking if they are equal, and not performing a placement.
And so you need to change this to:
em=groceryList[1] ;
If I understood your question correctly, then you want to repeatedly randomize an array index and log the results in an array. You can do it like this:
You basically push the item of the array located at the randomized index into the log. I am also displaying it in a div for illustrative purposes.