10 Array methods in JS with Doraemon Story

#chaicode

🏠 Story: "Doraemon and the Case of the Missing Gadget!"

One day, Nobita ran home crying because he had lost a secret gadget that Doraemon had given him! 😱

Now, it's up to Doraemon, Nobita, and friends to find it using JavaScript array methods!

1️⃣ push() – Listing Possible Locations

Doraemon and Nobita started listing places where the gadget could be.

let locations = [];
locations.push("Nobita's Room");
locations.push("School");
locations.push("Gian's House");
locations.push("Shizuka's Garden");
console.log(locations);

💡 push() adds elements to the end of an array.

2️⃣ pop() – Removing an Impossible Location

Nobita realized, "I didn’t go to Gian’s house today!" So they removed it.

jsCopyEditlocations.pop();
console.log(locations);

💡 pop() removes the last element from an array.


3️⃣ unshift() – Adding a Suspicious Place First

Doraemon suspected "Dekisugi’s Desk" because he saw Nobita there.

jsCopyEditlocations.unshift("Dekisugi’s Desk");
console.log(locations);

💡 unshift() adds elements to the beginning of an array.


4️⃣ shift() – Eliminating a Wrong Guess

Shizuka said, "It wasn't in my garden, I cleaned it this morning!"

jsCopyEditlocations.shift();
console.log(locations);

💡 shift() removes the first element from an array.


5️⃣ indexOf() – Checking if Nobita’s Room is on the List

Doraemon wanted to confirm if "Nobita's Room" was a location to check.

jsCopyEditconsole.log(locations.indexOf("Nobita's Room")); // Output: 0

💡 indexOf() finds the position of an item in an array.


6️⃣ includes() – Verifying a Location

Nobita asked, "Is my school on the list?"

jsCopyEditconsole.log(locations.includes("School")); // Output: true

💡 includes() checks if an array contains a specific value.


7️⃣ slice() – Checking the Top 2 Suspect Locations

Doraemon decided to prioritize checking the top 2 places.

jsCopyEditlet suspectLocations = locations.slice(0, 2);
console.log(suspectLocations); // ["Nobita's Room", "School"]

💡 slice() extracts a portion of an array without modifying it.


8️⃣ filter() – Finding the Most Likely Spot

Doraemon eliminated locations where the gadget couldn’t be.

jsCopyEditlet possibleLocations = locations.filter(place => place !== "Dekisugi’s Desk");
console.log(possibleLocations);

💡 filter() creates a new array with elements that match a condition.


9️⃣ map() – Assigning Search Tasks

Doraemon divided the search among friends.

jsCopyEditlet tasks = locations.map(place => `Search in ${place}!`);
console.log(tasks);

💡 map() transforms each element into something new.


🔟 find() – Identifying the Culprit!

Suddenly, they found the gadget in Suneo’s backpack!

jsCopyEditlet culprit = locations.find(place => place === "Suneo's Backpack");
console.log(`The gadget was in ${culprit}!`);

💡 find() returns the first matching element from an array.