Meal Maker Javascript Project challenge question

Hi guys. I’ve just completed the Meal Maker project and as described in step 11, I tried to challenge myself, by assigning the meal and price randomly from a meal/price array. I wanted the prices to be assigned respectively to meals, therefore I chose to use a nested array. I wanted also to keep on checking the value submitted to the program by the array, in case a future programmer was to push something to the array in the incorrect order.
Well I wrote the code, and it’s not working. I keep getting told that the values were submitted incorrectly. Below is my code. Could I ask for any suggestions please?

const menu = {
  _meal: '',
  _price: 0,
  set meal(mealToCheck) {

    if (typeof mealToCheck === 'string') {
      return this._meal = mealToCheck;
    };
  },
  set price(priceToCheck) {

    if (typeof priceToCheck === 'number') {
      return this._price = priceToCheck;
    };
  },
  get todaysSpecial() {
    if (this._meal && this._price) {
      return `Todays special is ${this._meal} for $${this._price}.`
    } else {return 'Meal or Price was not set correctly!'}
  }
};
let mealArray = [ ['pizza', 5], ['soup', 4],['steak', 10],['nachos', 6],['burger', 8] ];
let mealGenerator = () => {
  let randomMealNumber = Math.floor(Math.random()*5);
  switch (randomMealNumber) {
    case 0:
    return 
    menu.meal = mealArray[0][0]
    menu.price = mealArray[0][1];
    case 1: 
    return 
    menu.meal = mealArray[1][0]
    menu.price = mealArray[1][1];
    case 2: 
    return
    menu.meal = mealArray[2][0]
    menu.price = mealArray[2][1];
    case 3:
    return 
    menu.meal = mealArray[3][0]
    menu.price = mealArray[3][1];
    case 4:
    return 
    menu.meal = mealArray[4][0]
    menu.price = mealArray[4][1];
  }
}
console.log(menu.todaysSpecial);

Any help will be greatly appreciated!

You have a return keyword right after each case in a single line. Everything that comes after the return is not executed anymore. To impede fall-through, you could have the break keyword after the assignment.
Apart, a switch statement is not a very generic solution to assign meals and prices. And it is not random at all. So a for loop or forEach method would be more fit for your purpose.

1 Like

Hi, thank you for your answer, but I’m afraid I don’t fully understand :joy:
the return keyword is usually always the first in the code block, is it not? From what I read, return makes the break keyword redundant as it returns the value after it and the next cases in a switch statement aren’t even checked. Either way, I did follow my cases with a break - it hasn’t changed anything.
Also I don’t fully understand what do you mean that this process isn’t random. The whole meal selection is based on the variable randomMealNumber, which is after all a random number from 0-4 range? wouldn’t that make it random?
I’ll have a look at possibilities of using the loops or forEach method. still, do you not think taht my issue lies somewhere with the scope of the functions and variable?

The main problem is that you don’t call your function mealGenerator, so nothing is executed at all.

return stands in a single line. That means that nothing is returned from the block. If you wrote:

return menu.meal = mealArray[0][0]
menu.price = mealArray[0][1];

the assignment of menu.meal would be made – that of menu.price not.
You don’t need the return keyword here at all because you do not do anything with the returned value. You do the assignment within the code block of the switch case.

So you need to write:

menu.meal = mealArray[0][0];
menu.price = mealArray[0][1];
break;

And then call the function mealGenerator.

Yes, you are right, the assignment is random, but not generic. And you don’t prevent double entries in your menu.

1 Like

Right, I got you! That makes a lot of sense. Thank you for taking the time to explain this to me. This did make it work. Also, pretty dumb of me never to actually call the function :joy:
I also understand what you mean with this program not preventing double entries. This could be improved. Thanks for your help again!

2 Likes

I would also note that the switch is completely redundant.

You could just write:

const mealGenerator = () => {
    let randomMealNumber = Math.floor(Math.random()*mealArray.length)
    menu.meal = mealArray[randomMealNumber][0]
    menu.price = mealArray[randomMealNumber][1]
}

In addition, by setting the multiplier on the Math.random() to the length of the mealArray, you can add as many meals as you like to the array and you won’t have to change anything to make it work.

1 Like

This is actually a great idea! Super simple and does the job much better. Thanks for sharing :smile:

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.