-2

My randomItem() function is returning veggies as undefined. Any idea why?

data = {
  bread: ["white", "italian 5 grain", "whole wheat"],
  cheese: ["cheddar", "muenster", "provolone", "swiss", "white american", "yellow america"],
  meat: ["oven roasted turkey", "oven roasted beef", "maple glazed ham", "black forest ham", "buffalo chicken breast", "bologna", "corned beef", "salsalito turkey", ],
  veggies: ["banana peppers", "black olives", "garlic pickles", "cucumbers", "dill pickles", "green peppers", "jalapeno peppers", "lettuce", "onions", "spinach", "tomato", "salt", "black pepper", "oregano", "oil and vinegar"],
  condiments: ["honey mustard", "spicy mustard", "mayo"],
  extras: ["avocado", "hummus", "guacamole", "bacon"],
  bake: ["pressed", "toasted"]
}

const getRandom = (min, max) => {
  return Math.floor((Math.random() * (max - min) + 1) + min);
}

function randomItem(item) {
  const random = getRandom(0, item.length - 1);
  console.log(data.item[random]);
}

randomItem(veggies);

EDIT: My code actually returned 'Reference Error, veggies is not defined'

peachcore
  • 45
  • 5
  • If you really get `undefined` instead of a `ReferenceError` then please adjust the snippet to match your actual code. – Andreas May 13 '20 at 18:38
  • `veggies` was never defined. You probably mean `data.veggies`. – str May 13 '20 at 18:39
  • 1
    maybe `randomItem('veggies');` `function randomItem(item) { const random = getRandom(0, item.length - 1); console.log(data[item][random]); }` – teg_brightly May 13 '20 at 18:41
  • make veggies a string `randomItem("veggies")` since you access `data.item` inside randomItem. – scrappedcola May 13 '20 at 18:41
  • @scrappedcola That's not how JavaScript (objects) work. – Andreas May 13 '20 at 18:46
  • 1
    It seems you should use `data.veggies` instead of `veggies` and `data[item]` instead of `data.item`. If that is what you want, then this question is a duplicate of https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json and https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable. – str May 13 '20 at 18:47
  • @Andreas Sorry, it was a reference error. Looks I have sufficient information from all the replies to understand and adjust my code. Thanks! – peachcore May 13 '20 at 20:19

1 Answers1

0

Problem 1: veggies is not a variable name so has to be passed as a string but if you were to access it from the data object it would be data.veggies

Here's a fix:

data = {
  bread: ["white", "italian 5 grain", "whole wheat"],
  cheese: ["cheddar", "muenster", "provolone", "swiss", "white american", "yellow america"],
  meat: ["oven roasted turkey", "oven roasted beef", "maple glazed ham", "black forest ham", "buffalo chicken breast", "bologna", "corned beef", "salsalito turkey", ],
  veggies: ["banana peppers", "black olives", "garlic pickles", "cucumbers", "dill pickles", "green peppers", "jalapeno peppers", "lettuce", "onions", "spinach", "tomato", "salt", "black pepper", "oregano", "oil and vinegar"],
  condiments: ["honey mustard", "spicy mustard", "mayo"],
  extras: ["avocado", "hummus", "guacamole", "bacon"],
  bake: ["pressed", "toasted"]
}

var getRandom = (min, max) => {
  return Math.floor((Math.random() * (max - min) + 1) + min);
}

function randomItem(item) {
  const random = getRandom(0, item.length - 1);
  console.log(item[random]);
    //instead of logging data.item[random], just get the whole value from the argument
    }

    //This returns the values
    randomItem(Object.values(data.veggies));
Cypherjac
  • 859
  • 8
  • 17