My final result for the JS Meal Maker project

I’m studying the Full Stack course and I just finished the project Meal Maker. I went through the steps quite easily and then started to work out some ways to add a random meal of the day.

The first attempt was simply picking a random meal and price from two arrays but I didn’t feel challenged that much so I looked to make it a bit more complex. I planned to have the array manipulated so that every time an item was picked, it was also removed from the array, I kind of accomplish that, but got stuck on how to reset it back to the original array once empty.

Couldn’t figure out a way and then thought that was probably too much for me and maybe prone to issues, so I changed the plan and decided I wanted to print out the full list of daily meals for the entire week having the app pick the name of the week to combine with the actual meal, and also create messages in case the number of meals were more or less than 7 and also telling me how many meals I should have added or removed.

It took me half a day, but I’m really happy, not sure I wrote it in the best or most efficient way, but it does what I planned nevertheless.

Here is the code and feel free to give inputs or ask questions.

const menu = {
    _meal: "",
    _price: 0,
    //Let set the new value of meal only if it's a string.
    set meal(mealToCheck) {
        if (typeof mealToCheck === "string") {
            return (this._meal = mealToCheck);
        }
    },
    //Let set the new value of price only if it's a number.
    set price(priceToCheck) {
        if (typeof priceToCheck === "number") {
            return (this._price = priceToCheck);
        }
    },
    //If both setters are true, then return a message using them, otherwise return message saying the values are wrong.
    get todaysSpecial() {
        if (this._meal && this._price) {
            return `'s Special is ${this._meal}, for just ${this._price}£!!`;
        } else {
            return "Meal and Price wasn't entered correctly!!";
        }
    }
};

//Array for the meal options and respective prices.
let meals = [
    {/*1*/name: "Pizza", price: 9},
    {/*2*/name: "Steak", price: 13},
    {/*3*/name: "Pie", price: 11},
    {/*4*/name: "Roast", price: 14},
    {/*5*/name: "Moussaka", price: 9},
    {/*6*/name: "Lasagne", price: 10},
    {/*7*/name: "Tacos", price: 9}
];

//Shuffle function, creates two variables.
const shuffle = (array) => {
    let currentIndex = array.length,
        randomIndex;
    //The function starts from the array length and ends when reaches 0
    while (currentIndex !== 0) {
        //Assigns a random number to the variable, and each time it runs, it lowers the max number by 1.
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex--;
        //It swaps the current array element, with the one equivalent to the random number index.
        [array[currentIndex], array[randomIndex]] = [
            array[randomIndex],
            array[currentIndex]
        ];
    }
    return array;
}

meals = shuffle(meals);
const dayOfTheWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

//I've created a for loop to go through the new shuffled array.
for (let i = 0; i < meals.length; i++) {
    //In here I'm saying that if the number of meals is equal to the week days, the weekly menu will be printed.
    if (meals.length === dayOfTheWeek.length) {
        menu.meal = meals[i].name;
        menu.price = meals[i].price;
        console.log(dayOfTheWeek[i] + menu.todaysSpecial);
        //If the meals are less, it will print how many need to be added.
    } else if (meals.length < dayOfTheWeek.length) {
        console.log(`You need to add ${Math.abs(meals.length - dayOfTheWeek.length)} meal(s) to the list!!!`)
        break;
        //If the meals are more, than it will print how many need to be removed.
    } else if (meals.length > dayOfTheWeek.length) {
        console.log(`You need to remove ${Math.abs(meals.length - dayOfTheWeek.length)} meal(s) from the list!!!`)
        break;
    }
}

Added a bit more so that now it takes 4 different arrays and shuffles them, I’m actually quite happy with the result, considering my level and how much I’ve implemented from the steps of the actual project.

const menu = { _meal: "", _price: 0, //Let set the new value of meal only if it's a string. set meal(mealToCheck) { if (typeof mealToCheck === "string") { return (this._meal = mealToCheck); } }, //Let set the new value of price only if it's a number. set price(priceToCheck) { if (typeof priceToCheck === "number") { return (this._price = priceToCheck); } }, //If both setters are true, then return a message using them, otherwise return message saying the values are wrong. get todaySpecial() { if (this._meal && this._price) { return `'s Special is ${this._meal}, for just ${this._price}£!!`; } else { return "Meal and Price wasn't entered correctly!!"; } } }; //Array for the meal options and respective prices. let meals0 = [ {/*1*/name: "Pizza", price: 9}, {/*2*/name: "Steak", price: 11}, {/*3*/name: "Pie", price: 9}, {/*4*/name: "Roast", price: 12}, {/*5*/name: "Moussaka", price: 9}, {/*6*/name: "Lasagne", price: 8}, {/*7*/name: "Tacos", price: 8} ]; let meals1 = [ {/*1*/name: "Omelette", price: 7}, {/*2*/name: "Kebab", price: 10}, {/*3*/name: "Ratatouille", price: 8}, {/*4*/name: "Quiche", price: 8}, {/*5*/name: "Burgers", price: 11}, {/*6*/name: "Hot-Dogs", price: 8}, {/*7*/name: "Burritos", price: 9} ]; let meals2 = [ {/*1*/name: "Bourguignon", price: 10}, {/*2*/name: "Stew", price: 11}, {/*3*/name: "Fish&Chips", price: 12}, {/*4*/name: "Fried Chicken", price: 7}, {/*5*/name: "Cannelloni", price: 10}, {/*6*/name: "Crepes", price: 8}, {/*7*/name: "Gyros", price: 10} ]; let meals3 = [ {/*1*/name: "Greek Salad", price: 9}, {/*2*/name: "Seafood", price: 13}, {/*3*/name: "Falafel", price: 8}, {/*4*/name: "Wraps", price: 10}, {/*5*/name: "Souvlaki", price: 11}, {/*6*/name: "Shawarma", price: 10}, {/*7*/name: "Quesadilla", price: 9}, ]; //Shuffle function, creates two variables. const shuffle = (array) => { let currentIndex = array.length, randomIndex; //The function starts from the array length and ends when reaches 0 while (currentIndex !== 0) { //Assigns a random number to the variable, and each time it runs, it lowers the max number by 1. randomIndex = Math.floor(Math.random() * currentIndex); currentIndex--; //It swaps the current array element, with the one equivalent to the random number index. [array[currentIndex], array[randomIndex]] = [ array[randomIndex], array[currentIndex] ]; } return array; } let week = ["Week 1", "Week 2", "Week 3", "Week 4"]; let all_meals = [meals0, meals1, meals2, meals3]; const dayOfTheWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] //I've created a for loop to go through the new shuffled array. for (let w = 0; w < week.length; w++) { console.log("\n\n" + week[w] + "\n") let week_meals = shuffle(all_meals[w]); for (let i = 0; i < all_meals[w].length; i++) { //In here I'm saying that if the number of meals is equal to the week days, the weekly menu will be printed. if (week_meals.length === dayOfTheWeek.length) { menu.meal = week_meals[i].name; menu.price = week_meals[i].price; console.log(dayOfTheWeek[i] + menu.todaySpecial); //If the meals are less, it will print how many need to be added. } else if (week_meals.length < dayOfTheWeek.length) { console.log(`You need to add ${Math.abs(week_meals.length - dayOfTheWeek.length)} meal(s) to the list!!!`) break; //If the meals are more, than it will print how many need to be removed. } else if (week_meals.length > dayOfTheWeek.length) { console.log(`You need to remove ${Math.abs(week_meals.length - dayOfTheWeek.length)} meal(s) from the list!!!`) break; } } }