lundi 24 avril 2023

Optimized JavaScript Random Strings With Key-Value Pairs/For Loop

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.




Aucun commentaire:

Enregistrer un commentaire