Table of contents
- 1. push() - Assembling the Group:
- 2. pop() - Dealing with Cancellations:
- 3. includes() - Packing Essentials:
- 4. find() - Finding a Hotel:
- 5. at() - Remembering the Room Number:
- 6. map() - Calculating Sightseeing Costs:
- 7. concat() - Joining a Trekking Group:
- 8. unshift() - Letting Others Go First:
- 9. filter() - Choosing the Best Photos:
1. push()
- Assembling the Group:
We started with an empty array to keep track of everyone who was going. We used the push()
method to add each friend's name to the list.
let darjeelingTrip = [];
darjeelingTrip.push("Koushik");
darjeelingTrip.push("Hitesh");
darjeelingTrip.push("Anirud");
darjeelingTrip.push("Piyush");
darjeelingTrip.push("Akash");
darjeelingTrip.push("Mukul");
console.log(darjeelingTrip); // Output: ["Koushik", "Hitesh", "Anirud", "Piyush", "Akash", "Mukul"]
2. pop()
- Dealing with Cancellations:
Unfortunately, some friends had to drop out at the last minute due to personal reasons. We used the pop()
method to remove their names from the list.
darjeelingTrip.pop(); // Mukul had to cancel
darjeelingTrip.pop(); // Akash also couldn't make it
console.log(darjeelingTrip); // Output: ["Koushik", "Hitesh", "Anirud", "Piyush"]
3. includes()
- Packing Essentials:
Before leaving, we made a list of essential items to pack. We used the includes()
method to check if we had everything.
let thingsToPack = ["jacket", "sweater", "camera", "first-aid kit"];
console.log(thingsToPack.includes("jacket")); // Output: true
console.log(thingsToPack.includes("raincoat")); // Output: false
4. find()
- Finding a Hotel:
After arriving in Darjeeling, we needed to find a good hotel. We used the find()
method to locate a hotel with available rooms.
let hotels = [
{ name: "Hotel Himalaya", available: false },
{ name: "Hotel Red Panda", available: true },
{ name: "Hotel Mount View", available: false },
];
let availableHotel = hotels.find(hotel => hotel.available === true);
console.log(availableHotel); // Output: { name: "Hotel Red Panda", available: true }ode
5. at()
- Remembering the Room Number:
After checking in, we got our room number. We used the at()
method (or direct indexing) to remember it.
let roomNumbers = ["101", "102", "103", "104"];
let ourRoom = roomNumbers[1]; // Or roomNumbers.at(1);
console.log(ourRoom); // Output: "102"
6. map()
- Calculating Sightseeing Costs:
We wanted to visit several places. We used the map()
function to calculate the total cost of sightseeing tickets for everyone.
let ticketPrices = [100, 50, 75, 120]; // Prices of different tickets
let totalCost = ticketPrices.map(price => price * darjeelingTrip.length).reduce((sum, price) => sum + price, 0);
console.log(totalCost); // Output: The total cost of tickets
7. concat()
- Joining a Trekking Group:
We decided to join a trekking group. We used concat()
to combine our group with another group of trekkers.
let ourGroup = ["Koushik", "Hitesh", "Anirud", "Piyush"];
let otherGroup = ["Bulu", "Simran"];
let trekkingGroup = ourGroup.concat(otherGroup);
console.log(trekkingGroup); // Output: ["Koushik", "Hitesh", "Anirud", "Piyush", "Bulu", "Simran"]
8. unshift()
- Letting Others Go First:
While waiting in line for the ropeway, we saw some elderly people. We used unshift()
to add them to the front of the line.
let ropewayLine = ["Koushik", "Hitesh", "Anirud", "Piyush"];
let elderlyPeople = ["Mr. Das", "Mrs. Roy"];
ropewayLine.unshift(...elderlyPeople);
console.log(ropewayLine); // Output: ["Mr. Das", "Mrs. Roy", "Koushik", "Hitesh", "Anirud", "Piyush"]
9. filter()
- Choosing the Best Photos:
After the trip, we had many photos. We used filter()
to select the best ones for our album.
let photos = [
{ location: "Tiger Hill", rating: 9 },
{ location: "Batasia Loop", rating: 8 },
{ location: "Peace Pagoda", rating: 7 },
{ location: "Tiger Hill", rating: 10 },
];
let bestPhotos = photos.filter(photo => photo.rating > 8);
console.log(bestPhotos); // Output: The photos with rating above 8
This Darjeeling trip was truly memorable, and JavaScript helped us organize and enjoy it to the fullest. It shows how even simple coding concepts can be useful in everyday situations.