Table of contents
(Part-1 )
After mastering the magic pot (array methods), Aryan's Paneer Butter Masala became the talk of Codepur. Everyone loved his dishes, and even the Maharaja (King) of Codepur sent an invitation to cook for the royal feast! 👑✨
But there was a twist…
Daadi Maa handed Aryan an old, dusty cookbook 📜.
"Beta, this contains the secret to advanced magic pot techniques*! If you master these, your cooking (JavaScript skills) will be unmatched."*
So, Aryan dived into advanced array methods while preparing for the feast!
1️⃣ The Royal Guest List: .forEach()
vs .map()
📜
The Maharaja wanted a list of all guests attending the feast. Aryan had the guest names stored in an array:
let guests = ['Ramesh', 'Suresh', 'Priya', 'Aisha'];
🔹 .forEach()
– Just Reads the List
"Beta, read each guest’s name and greet them."
guests.forEach(guest => console.log(`Welcome, ${guest}!`));
Output:
Welcome, Ramesh!
Welcome, Suresh!
Welcome, Priya!
Welcome, Aisha!
🔹 .map()
– Creates a Fancy List
"Instead of just reading names, create an official Invitation Card*."*
let invitations = guests.map(guest => `Dear ${guest}, you are invited to the royal feast!`);
console.log(invitations);
Output:
[
'Dear Ramesh, you are invited to the royal feast!',
'Dear Suresh, you are invited to the royal feast!',
'Dear Priya, you are invited to the royal feast!',
'Dear Aisha, you are invited to the royal feast!'
]
👑 Lesson: .forEach()
just executes a function for each item, while .map()
creates a new array with transformed values!
2️⃣ The Missing Ingredients Mystery: .find()
& .findIndex()
🕵️♂️
While preparing the dishes, Aryan realized he was missing an ingredient!
🔹 .find()
– Find the First Matching Item
"Beta, find the first spice that starts with ‘M’*."*
let spices = ['Haldi', 'Mirchi', 'Jeera', 'Methre', 'Methi'];
let firstMSpice = spices.find(spice => spice.startsWith('M'));
console.log(firstMSpice); // 'Mirchi'
🔹 .findIndex()
– Find the Position of an Item
"Where is ‘Jeera’ in the spice list?"
let jeeraIndex = spices.findIndex(spice => spice === 'Jeera');
console.log(jeeraIndex); // 2
3️⃣ The Royal Feast Order: .some()
vs .every()
🍽️
The royal guards checked if the food was safe. They had two questions:
🔹 .some()
– Is at least one dish spicy? 🌶️
"Does any dish contain ‘Mirchi’?"
let dishes = ['Paneer Butter Masala', 'Dal Tadka', 'Aloo Gobi', 'Mirchi Bhaji'];
let isSpicy = dishes.some(dish => dish.includes('Mirchi'));
console.log(isSpicy); // true
🔹 .every()
– Are all dishes vegetarian? 🥗
"Check if all dishes are pure veg*."*
let vegDishes = ['Dal Makhani', 'Paneer Tikka', 'Kadhi', 'Biryani'];
let allVeg = vegDishes.every(dish => dish !== 'Chicken');
console.log(allVeg); // true
👑 Lesson: .some()
checks if at least one item meets the condition, while .every()
ensures all items pass the test.
4️⃣ Cooking in Batches: .reduce()
🏺
Aryan had to calculate the total cost of ingredients. Instead of adding one by one, he used .reduce()
to sum them up!
let ingredientPrices = [10, 15, 25, 30, 20]; // Prices in INR
let totalCost = ingredientPrices.reduce((sum, price) => sum + price, 0);
console.log(`Total Cost: ₹${totalCost}`); // Total Cost: ₹100
👑 Lesson: .reduce()
accumulates values into a single result (e.g., sum, total).
5️⃣ The Maharaja’s Special Dish: .flat()
& .join()
🍛
For the final dish, Aryan needed to combine all ingredients properly.
🔹 .flat()
– Flatten Nested Arrays
"Beta, sometimes ingredients come in small packets, and you need to open them."
let allIngredients = [['Haldi', 'Mirchi'], ['Jeera', 'Elaichi'], ['Dhaniya']];
let finalIngredients = allIngredients.flat();
console.log(finalIngredients); // ['Haldi', 'Mirchi', 'Jeera', 'Elaichi', 'Dhaniya']
🔹 .join()
– Make a Beautiful Recipe String
"Write all ingredients in a nice sentence."
let recipe = finalIngredients.join(', ');
console.log(`Final Recipe: ${recipe}`);
// Output: "Final Recipe: Haldi, Mirchi, Jeera, Elaichi, Dhaniya"
The Grand Feast 🎉
At last, Aryan completed the feast, and the Maharaja was delighted!
"Beta, your magic pot techniques are extraordinary! You have truly mastered the art of Arrays in JavaScript*."*
Aryan smiled and said,
"Daadi Maa, your ancient recipe book (array methods) made me a great chef (coder)!"
With JavaScript arrays mastered, Aryan’s coding skills improved. He was now ready for even bigger challenges in Codepur! 🚀
✨ Moral of the Story ✨
Just like cooking requires proper organization, filtering, and transformation, handling arrays in JavaScript efficiently can make your code cleaner and more powerful!
💡 What’s Next?
Would you like to learn objects in JavaScript in the next story? Let Checkout my next article! 😊