Home > database >  How to array target in a loop
How to array target in a loop

Time:06-07

I've recently had some help adding randomisation on my practice project app and now I'm trying to add the last functionality on it but I can't get around.

I have this app that shuffles 7 objects from an array and presents them as a meal of the day for the entire week. For now, I've been able to have that same array logged 4 times and each of the 4 instances is shuffled from the other, but I've added other 3 arrays for a total of 4, and I'd like to have the app shuffling and logging each of those 4 arrays once.

English is not my primary language hopefully I've been clear enough.

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}
];
let meals1 = [
    {/*1*/name: "Omelette", price: 9},
    {/*2*/name: "Kebab", price: 13},
    {/*3*/name: "Ratatouille", price: 14},
    {/*4*/name: "Quiche", price: 9},
    {/*5*/name: "Burgers", price: 9},
    {/*6*/name: "Hot-Dogs", price: 10},
    {/*7*/name: "Burritos", price: 9}
];
let meals2 = [
    {/*1*/name: "Bourguignon", price: 10},
    {/*2*/name: "Stew", price: 14},
    {/*3*/name: "Fish&Chips", price: 15},
    {/*4*/name: "Fried Chicken", price: 10},
    {/*5*/name: "Cannelloni", price: 10},
    {/*6*/name: "Crepes", price: 11},
    {/*7*/name: "Gyros", price: 10}
];
let meals3 = [
    {/*1*/name: "Greek Salad", price: 11},
    {/*2*/name: "Seafood", price: 15},
    {/*3*/name: "Falafel", price: 16},
    {/*4*/name: "Wraps", price: 11},
    {/*5*/name: "Souvlaki", price: 11},
    {/*6*/name: "Shawarma", price: 12},
    {/*7*/name: "Quesadilla", price: 11}
]

//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"];
const dayOfTheWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
// meals = shuffle(meals);

//I've created a for loop to go through the new shuffled array.

for (let w = 0; w < week.length; w  ) {
    console.log("\n"   week[w])
    meals = shuffle(meals);
    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;
        }
    }
}

CodePudding user response:

Put all the meals arrays in another array:

let all_meals = [meal, meals1, meals2, meals3];

Then you can use all_meals[w] to get the meals for that week.

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.
//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}
];
let meals1 = [
    {/*1*/name: "Omelette", price: 9},
    {/*2*/name: "Kebab", price: 13},
    {/*3*/name: "Ratatouille", price: 14},
    {/*4*/name: "Quiche", price: 9},
    {/*5*/name: "Burgers", price: 9},
    {/*6*/name: "Hot-Dogs", price: 10},
    {/*7*/name: "Burritos", price: 9}
];
let meals2 = [
    {/*1*/name: "Bourguignon", price: 10},
    {/*2*/name: "Stew", price: 14},
    {/*3*/name: "Fish&Chips", price: 15},
    {/*4*/name: "Fried Chicken", price: 10},
    {/*5*/name: "Cannelloni", price: 10},
    {/*6*/name: "Crepes", price: 11},
    {/*7*/name: "Gyros", price: 10}
];
let meals3 = [
    {/*1*/name: "Greek Salad", price: 11},
    {/*2*/name: "Seafood", price: 15},
    {/*3*/name: "Falafel", price: 16},
    {/*4*/name: "Wraps", price: 11},
    {/*5*/name: "Souvlaki", price: 11},
    {/*6*/name: "Shawarma", price: 12},
    {/*7*/name: "Quesadilla", price: 11}
];

let all_meals = [meals, meals1, meals2, meals3];

//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"];
const dayOfTheWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
// meals = shuffle(meals);

//I've created a for loop to go through the new shuffled array.

for (let w = 0; w < week.length; w  ) {
  console.log("\n"   week[w])
  let week_meals = shuffle(all_meals[w]);
  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 (week_meals.length === dayOfTheWeek.length) {
      menu.meal = week_meals[i].name;
      menu.price = week_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 (week_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 (week_meals.length > dayOfTheWeek.length) {
      console.log(`You need to remove ${Math.abs(week_meals.length - dayOfTheWeek.length)} meal(s) from the list!!!`)
      break;
    }
  }
}

  • Related