Table of contents
- How all the mess processes works :
- Mess manager needs all the registered students list in the form of an array.
- every() : Checking all the registered students are valid or not .
- filter() : Filtering out the outdated registrations.
- push() : To register a new student.
- pop() : To remove the last registered student.
- includes() : To check whether a student is registered or not.
- unshift() : To add a new dish in today’s menu.
- slice() : To get some portion of array.
- reduce() : To get the total expense of a month.
- indexOf() : To find a registered student registration number.
- filter() : To filter out the vegeterian and non - vegeterian students.
All of us have mess in our colleges. But their is a huge issue of food shortage that is some registered student does not get the food because of shortage of food. This shortage occurs due to some students who are not registerd in the mess but they take meal from mess illegally or some registerd students take meal twice or thrice or many more times according to their impish level. To overcome this solution, our mess manager came up with an idea of applying array methods behind the scene of mess processes.
And after this mess manager start working on this idea and eventually he found a big relief from the above expalined problem.
How all the mess processes works :
Mess manager needs all the registered students list in the form of an array.
He created an array named Registerd Students in which he stored all the students who opted for the mess submitted fees for the mess.
let registeredStudents = ["Rohan", "Shashank Shekhar", "Rudraksh Dixit", "Osank Verma", "Yash Raj"];
every() : Checking all the registered students are valid or not .
If mess manager has to check that registered students have days left to eat or not in the mess according to their submitted fees, he uses every() method. (He checks once in a month - in the starting of every month)
let registeredStudents = ["Rohan", "Shashank Shekhar", "Rudraksh Dixit", "Osank Verma", "Yash Raj"];
const isDaysLeft = (daysLeft) => daysleft > 30;
console.log(registeredStudents.every(isDaysLeft));
filter() : Filtering out the outdated registrations.
If mess manager wants to remove the expired registration, he uses filter() method.
let registeredStudents = ["Rohan", "Shashank Shekhar", "Rudraksh Dixit", "Osank Verma", "Yash Raj"];
registeredStudents.filter(daysLeft => {
if(daysLeft <= 0){
// Remove that student from the registerd list.
}
});
push() : To register a new student.
If a new student want to join mess, then with the help of push() method of array manager register that student.
let registeredStudents = ["Rohan", "Shashank Shekhar", "Rudraksh Dixit", "Osank Verma", "Yash Raj"];
registeredStudents.push("Amaan Shafi Hyder");
pop() : To remove the last registered student.
If the last registerd student want to cancel the mess subscription, then pop() method of array comes into the picture.
let registeredStudents = ["Rohan", "Shashank Shekhar", "Rudraksh Dixit", "Osank Verma", "Yash Raj", "Amann Shafi Hyder"];
registeredStudents.pop();
console.log(registeredStudents);
// Output will be : registeredStudents ["Rohan", "Shashank Shekhar", "Rudraksh Dixit", "Osank Verma", "Yash Raj"]
includes() : To check whether a student is registered or not.
If mess manager is not able to recognise a stue=dent from his face because he came after a long time to eat in the mess because he/she was on a leave, then he will check the registered list of students with the includes() array method.
let registeredStudents = ["Rohan", "Shashank Shekhar", "Rudraksh Dixit", "Osank Verma", "Yash Raj", "Amann Shafi Hyder"];
console.log(registeredStudents.includes("Yash Raj"));
// Expected Output : True
unshift() : To add a new dish in today’s menu.
Suppose mess manager have another array of dishes of today’s menu and he wants to add a new dish in today’s menu due to some reason, then he will take the help of unshift() array metod.
const dishes = ["Seasonal Sabzi", "Raita/Salad", "Roti/Rice"];
dishesh.shift("Lassi");
console.log(dishes);
// Expected output : dishes ["Lassi","Seasonal Sabzi", "Raita/Salad", "Roti/Rice"]
slice() : To get some portion of array.
Once mess manager thought about giving a special meal to the first five registered students, that,s why he chooses first five students from the registered students list with the help of slice array method.
let registeredStudents = ["Rohan", "Shashank Shekhar", "Rudraksh Dixit", "Osank Verma", "Yash Raj", "Amann Shafi Hyder"];
console.log(`Special meal will be given to : ${registeredStudents.slice(0, 5)}`);
reduce() : To get the total expense of a month.
For the monthly expenses, mess manager maintains another array named expenses which stores the daily expenses according to the menu of respective days.
If manager wants to calculate a month expense then he will use reduce array method.
let expenses = [5400, 6570, 2450, 3000, 3560, 4300];
const initialValue = 0;
const totalExpense = expenses.reduce((accumulator, currentValue) => accumulator + currentValue,
initialValue,
);
console.log(totalExpense);
// Expected Output : 5400 + 6570 + 2450 + 3000 + 3560 + 4300
// i.e., 25280
indexOf() : To find a registered student registration number.
To find the day of registration of a student and the remaining days of his/her subscription, mess manager will use the indexOf() array method.
let registeredStudents = ["Rohan", "Shashank Shekhar", "Rudraksh Dixit", "Osank Verma", "Yash Raj", "Amann Shafi Hyder"];
conole.log((registeredStudents.indexOf("Rudraksh Dixit")) + 1);
// Expected Output : 3
filter() : To filter out the vegeterian and non - vegeterian students.
If today’s menu contain a non-veg dish then to calculate the non-vegeterian students mess manager will use filter method.
let registeredStudents = ["Rohan", "Shashank Shekhar", "Rudraksh Dixit", "Osank Verma", "Yash Raj", "Amann Shafi Hyder"];
const non_veg = registeredStudents.filter((Student) => student == "Non - Vegeterian");
console.log(non_veg);
I am wrapping up this topic by informing you that my college mess has tackled the issue of food scarcity caused by some mischievous students by using these array methods. Let me know how your college mess is doing. Will you suggest your mess manager use these methods to solve similar problems?