var crustType = ["wheat", "hand tossed", "deep dish"];
var crustTypeArr = crustType[Math.floor(Math.random() * crustType.length)];
var sauceType = ["marinara", "tomato", "none"];
var sauceTypeArr = sauceType[Math.floor(Math.random() * sauceType.length)];
var cheeses = ["mozarella", "cheddar", "feta"];
var chessesArr = cheeses[Math.floor(Math.random() * cheeses.length)];
var toppings = ["vegan sausage", "spinach", "onions", "pepperoni", "mushrooms"];
var toppingsArr = toppings[Math.floor(Math.random() * toppings.length)]
function randomPizza(crustType, sauceType,cheeses, toppings) {
randomPizza.crustType = crustType;
randomPizza.sauceType = sauceType;
randomPizza.cheeses = cheeses;
randomPizza.toppings = toppings;
return randomPizza;
}
var p5 = randomPizza(crustTypeArr, sauceTypeArr, chessesArr, toppingsArr);
console.log(p5);
How would you do this with key value pairs/for loop, or whichever way you think is more optimized if there were like a million variables?
I got the right code, but it is not optimized.
2
Answers
You can add the options into an object and don’t pass anything to your function. Instead loop through the options within the function and randomize from there adding to the object.
I think what you mean by optimize is really making it work with an configurable amount of ingredients. You can do so using the following approach.
I am using
Object.entries()
to read from the catalog andreduce()
to build thepizza
object.