Contra-script, Javascript Array and Methods

Declaration of Array
This is the year 1987; we are at Jungle base.
let level1 = ['grunt', 'grunt', 'flying-soldier', 'grunt'];
// we need to engage now
console.log(level1, "Engage");
Methods in arrays
Shift()
Our hero, Bill needs to take out a few enemies. here we use the shift method, which removes element from the start.
level.shift();
level.shift();
console.log(level1) // output is level1=['flying-soldier', 'grunt'] 'grunt' and 'grunt' are terminated
Push()
Now we go ahead in the level, and new enemies appear, here we use the push method, so that we can insert new elements in the level1
level1.push("Soldier-sniper", "scuba-soldier", "Boss");
console.log(level1); // here we get level1=['flying-soldier', 'grunt', 'Soldier-sniper', 'scuba-soldier', 'Boss']
indexOf()
Now I want to locate where is the enemy in the jungle stage, so for this, we will use the index of method which will give me the position of the ‘Boss’ in the level1 array
console.log(level1.indexof("Boss"); // the index will be 4. so my final enemy is located at the last.
Concat()
Now we have to give the bill a power, but it is in a different section of the level, we need to bring it to level 1, how can we do that, We use the concat method, which will merge two arrays
let powerups = ['S-wide range bullets', 'L Laser-gun', 'grunt'];
console.log(level1.concat(powerups)); // here we get level1=['flying-soldier', 'grunt', 'Soldier-sniper", 'scuba-soldier', 'Boss', 'S-wide range bullets', 'L Laser-gun']
filter()
Now we must remove Grunt from the level 1 jungle, so what should we do? we should perform the filter method; it will return a new array which passes the test case
let onlyGrunts = level1.filter((e) => e === 'grunt'); // it will filter out grunts
pop()
Now we need to eliminate mid-level enemies like “scuba soldiers”, and it is placed at the end of the enemies array, how will we do that, we will use the pop method, pop removes the last element in the array
let enemies = ['Grunts', 'Soldier-sniper', 'Scuba-soldiers'];
console.log(enemies.pop()); // output is the last element ie the Scuba-soldier is removed from the enemies list
includes()
Now, Bill needs to check if the enemies contain sniper-soldier and how he will be able to do that. we use the includes method, This will check if the array consists of that specific value or not
console.log(enemies.includes('Soldier-sniper')); // This will give the result as true to Bill./
map()
We need to describe each element in the level1 array like what bill has encountered
// let level1 = ['grunt', 'grunt', 'flying-soldier', 'grunt'];
console.log(level1.map((item => item + ' encountered!')); // let level1 = ['grunt encountered', 'grunt encountered', 'flying-soldier encountered', 'grunt encountered'];
slice()
now Bill needs to extract a portion of the level, we can achieve this by using the slice method, which extracts a section of an array and returns a new array
let levelSection = level1.slice(0, 2); // Extracts elements from index 0 to 2
console.log(levelSection);
unshift()
Now we need to add some staring area like a wall in the level1 array, how are we going to achieve it by using the unshift method, Adds one or more elements to the beginning of an array, like adding a new starting area to the level
level1.unshift('wall', 'wall');
console.log(level1) // The output will be level1 = ['wall', 'wall', 'grunt encountered', 'grunt encountered', 'flying-soldier encountered', 'grunt encountered'];
Conclusion
Here I have tried to explain few method of arrays in javascript in a fun way.




