Naruto Uzumaki, the energetic ninja from Hidden Leaf Village, starts a new mission to learn JavaScript Array Methods. On the way, he meets friends, fights enemies, and improves his skills using these powerful methods or functions.
A New Mission Begins (push)
As we know, Naruto's training never stops. As he always prepares for a new mission, he decides to add some new Jujustu techniques to his skill set. Using the push() method, he expands his abilities.
let narutoSkills = ['Shadow Clone Jutsu', 'Rasengan'];
narutoSkills.push('Sage Mode', 'Rasenshuriken');
console.log(narutoSkills);
// Output: ['Shadow Clone Jutsu', 'Rasengan', 'Sage Mode', 'Rasenshuriken']
With new techniques in hand, Naruto is ready for his journey.
Leaving the Past Behind (pop)
During training, Naruto realizes that one of his old techniques is no longer effective, so he decides to remove the least useful move from his arsenal using the pop() method.
let removeSkill = narutoSkills.pop();
console.log(narutoskills);
//Output: ['Shadow Clone Jutsu', 'Rasengan', 'Sage Mode']
console.log(`Naruto let go of: ${removeSkill}`);
//Output: Naruto let go of: Rasenshuriken
Letting go of the Rasenshuriken for now, he focuses on perfecting the remaining skills.
A Comrade Departs (shift)
During his mission , Naruto’s team undergoes a major change. Sakura , needing to tend to medical duties, temporarily leave Team-7 . Usign shift() , Naruto update his teamlist:
let team7 = ['Sakura', 'Naruto', 'Sasuke'];
let departedNumber = team7.shift();
console.log(team7);
//Output: ['Naruto', 'Sasuke']
console.log(`${departedNumber} has left Team 7`);
//Output: Naruto has left Team 7.
A New Ally Joins (unshift)
A mission will never be complete without a strong team! To maintain balance, Kakashi assigns Sai to Team 7. Naruto uses unshift() to welcome their new member.
team7.unshift('Sai');
console.log(team7);
// Output: ['Sai', 'Naruto', 'Sasuke']
With a new formation, Team 7 is ready for action.
Training and Leveling Up (map)
Naruto is decide to improve his team’s abilities. Using map() , he enhance their strengths:
let trainingLevels = [5, 6, 7];
let newTrainingLevels = trainingLevels.map(level => level + 2);
console.log(newTrainingLevels);
// Output: [7, 8, 9]
After intence training all team member is now more stronger .
Choosing the Right Techniques (filter)
Not every jutsu is suited for every battle. When facing dangerous and powerful enemies, Naruto decides to filter out weaker techniques and keep only the most powerful jutsu.
let MostPowerfulTechniques = narutoSkills.filter(skill => skill === 'Shadow Clone Jutsu') || skill === 'Sage Mode');
console.log(bestTechniques);
// Output: ['Shadow Clone Jutsu', 'Sage Mode']
Finding Sasuke (find)
Sasuke is go for another work, and without Sasuke mission battle not complete so bring Sasuke back to the village. He searched for him using find():
let rogueNinjas = ['Orochimaru', 'Kabuto', 'Sasuke', 'Madara'];
let sasuke = rogueNinjas.find(ninja => ninja === 'Sasuke');
console.log(`Found: ${sasuke}`);
// Output: Found: Sasuke
Preparing for Battle (forEach)
Before going to battle, Naruto decides to go over the final strategies. He uses forEach() to check their weapons and jutsu:
team7.forEach(member => console.log(`${member} is ready for battle!`));
// Output:
// Sai is ready for battle!
// Sasuke is ready for battle!
// Sakura is ready for battle!
Gathering Chakra (reduce)
To perform a massive Rasengan , Naruto must gather chakra from all his clones usings reduce(), he combines their strength into one ultimate attack:
let cloneChakras = [10, 20, 30, 40];
let totalChakra = cloneChakras.reduce((total, chakra) => total + chakra, 0);
console.log(`Total Chakra Gathered: ${totalChakra}`);
// Output: Total Chakra Gathered: 100
Checking for Injuries (some)
After the battle, Naruto checks to see if any of his teammates are injured using some():
let teamStatus = [false, false, true];
let hasInjured = teamStatus.some(status => status === true);
console.log(hasInjured ? 'We have an injured teammate!' : 'Everyone is safe.');
// Output: We have an injured teammate!
Sakura quickly steps in to heal, ensuring the team can continue their journey.