Since yesterday was Saraswati Puja, I found myself remembering my school days. It made me think about one of the most significant parts of school life — the School Assembly. A place filled with memories, incidents, and events that we often tend to overlook.
But today, let us relive the school assembly while also learning some array methods using the same analogy. Ready? Let's dive in!
Triiiiing Tringggg !! 🔔
The bell rings, and it's time for the assembly. So what's the first thing you do after hearing the bell? Naturally, you line up with your class, right?
Now, imagine that the line you are forming represents an array data structure.
So, the first thing you do after you get into line is to arrange yourself in the order of height. But wait, who is to ensure this? It's your teacher, of course.
schoolAssembly.sort()
Just like you have teachers to ensure that the line is in the correct order, arrays have the .sort()
method to arrange their elements.
The sort method of an array arranges the array members in ascending order.
let schoolAssembly = ['Hitesh', 'Mukul', 'Piyush', 'Anirudh', 'Nishu', 'Sharma Ji Ka Launda', 'Samyak'];
schoolAssembly.sort();
// Output: ['Anirudh', 'Hitesh', 'Mukul', 'Nishu', 'Piyush', 'Samyak', 'Sharma Ji Ka Launda']
But as the students are sorted, the teacher notices that the shortest student is still at the back of the line because he had some taller friends standing behind him. So, what does the teacher do? She moves that student to the front of the line.
schoolAssembly.pop()
First, she removes the student from the last position, similar to how .pop()
removes the last element from the array and returns it.
let removedStudent = schoolAssembly.pop();
console.log(removedStudent);
// Output: 'Sharma Ji Ka Launda' (the last student is removed)
schoolAssembly.unshift(student)
Next, she places the student at the front of the line. This is similar to the .unshift(student)
method in arrays, which adds an element to the beginning of an array.
schoolAssembly.unshift('Sharma Ji Ka Launda');
// Output: ['Sharma Ji Ka Launda', 'Anirudh', 'Hitesh', 'Mukul', 'Nishu', 'Piyush', 'Samyak']
Now that everyone is arranged neatly, the principal arrives to give a speech. During the speech, he mentions "Sharma Ji Ka Launda", who recently won a competition. So, the teacher of his class needs to find him from the assembly line and send him to the stage.
schoolAssembly.indexOf(“SharmaJiKaLaunda“)
To find where the student is, the teacher looks up the assembly line and finds that he is at the 5th position. Similar to this, indexOf
gives the index of where the element is in the array.
let position = schoolAssembly.indexOf('Sharma Ji Ka Launda');
console.log(position);
// Output: 0 (Sharma Ji Ka Launda is at the 1st position)
After distributing the prize, principal then talks about the fight that happened between two boys Mukul and Akash regarding some girl “Nishu”.
So, the teacher from grade goes to their respective assembly line to check if it contains that group of students.
schoolAssembly.includes(“Mukul“)
Similar to how the teacher checks for students in the assembly line, the .includes()
method checks for elements in the array and returns true
or false
accordingly.
let hasMukul = schoolAssembly.includes('Mukul');
console.log(hasMukul);
// Output: true
let hasAkash = schoolAssembly.includes('Akash');
console.log(hasAkash);
// Output: false as the schoolAssembly doesn't contain Akash
The teacher rants about the situation to the students and tells them not to repeat it again. After all the incidents and rant from the principal, the Mukul go and sit at the back of the assembly line.
schoolAssembly.push('Mukul')
This is similar to how the .push()
method works in arrays, adding the elements back to the array.
schoolAssembly.push('10 C students');
console.log(schoolAssembly);
// Output: ['Sharma Ji Ka Launda', 'Anirudh', 'Hitesh', 'Mukul', 'Nishu', 'Piyush', 'Samyak', '10 C students']
After the prayer and national anthem, the most frightening thing for us as grown-up kids — the checking.
Checking of hair, nails, and all.
Do we have a method for this?
schoolAssembly.every(isNailsCut)
Yes, we do! It’s the .every()
method. Just like how everyone should be checked (without any favoritism) during the school assembly, the .every()
method checks every member of the array for the given condition.
const isNailsCut = (students) => student.nails < 2;
let allNailsCut = schoolAssembly.every(isNailsCut);
console.log(allNailsCut);
// Output: true or false depending on whether all students pass the "nails cut" check
schoolAssembly.slice(1, 3)
The teacher spots a student with long hair at the back of the assembly line and instructs them to sit outside, while the rest of the students proceed to class.
This is similar to how the .slice()
method selects a specific portion of an array without changing the original array.
let longHairGroup = schoolAssembly.slice(1, 3);
console.log(longHairGroup );
// Output: ['Anirudh', 'Hitesh']
Now that checking is done and the assembly is finished, students can proceed ahead to their respective class. But since class 9 C is opposite, the assembly students of that line need to reverse.
schoolAssembly.reverse()
The students reverse, and each student starts to go to the class from the beginning.
The reversing can be achieved in array using .reverse()
method.
schoolAssembly.reverse();
console.log(schoolAssembly);
// Output: ['10 C students', 'Samyak', 'Piyush', 'Nishu', 'Mukul', 'Hitesh', 'Anirudh', 'Sharma Ji Ka Launda']
Finally, the assembly ends, and it's time for students to go to their respective classes. The line gets smaller as one student leaves.
schoolAssembly.shift()
Just like how the first student in line goes to the class, the .shift()
method removes the first element from the array and returns it.
let firstStudent = schoolAssembly.shift();
console.log(firstStudent);
// Output: '10 C students' (the first student is removed and goes to the class)
And with that, the assembly ends, and everyone proceeds to their respective destinations. The methods of arrays, just like the activities of an assembly, help us stay organized, whether it's sorting, checking, adding, or removing elements. Just like our school assembly was an organized process, arrays too are best when we know how to use their methods efficiently.
Triiing Triinggg, that’s the bell. We are done for the day!
If you find the blog interesting consider dropping a like and a follow !!