skip to Main Content
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


  1. 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.

    const options = {
      crustType: ["wheat", "hand tossed", "deep dish"],
      sauceType: ["marinara", "tomato", "none"],
      cheeses: ["mozarella", "cheddar", "feta"],
      toppings: ["vegan sausage", "spinach", "onions", "pepperoni", "mushrooms"]
    
    };
    
    function randomPizza() {
      let pizza = {};
      
      for(key in options){
        pizza[key] = options[key][Math.floor(Math.random() * options[key].length)]
      }
    
      return pizza;
    }
    
    var p5 = randomPizza();
    
    console.log(p5);
    Login or Signup to reply.
  2. 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 and reduce() to build the pizza object.

    const crustType = ["wheat", "hand tossed", "deep dish"];
    const sauceType = ["marinara", "tomato", "none"];
    const cheeses = ["mozarella", "cheddar", "feta"];
    const toppings = ["vegan sausage", "spinach", "onions", "pepperoni", "mushrooms"];
    
    function randomPizza(ingredientCatalog) {
      // loop over the catalog and 
      // select one random option for each type
      return Object.entries(ingredientCatalog)
      .reduce((pizza, [ingredientType, ingredientList]) => (pizza[ingredientType] = ingredientList[Math.floor(Math.random() * ingredientList.length)], pizza), {});
    }
    
    // catalog of ingredient types and ingredients
    const catalog = {
      crustType,
      sauceType,
      cheeses,
      toppings
    };
    
    // generate 5 random pizzas
    console.log([...new Array(5)].map(_ => randomPizza(catalog)))
    .as-console-wrapper {
        max-height: 100% !important;
        top: 0;
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search