-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrayPosition.js
29 lines (23 loc) · 1.11 KB
/
arrayPosition.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
You are given an array:
var fruits = ["Apple", "Banana", "Orange"];
a) Find the index of 'Banana' and replace 'Banana' with 'Mango'.
b) Remove 'Orange' and add 'Watermelon'.
*/
var fruits = ["Apple", "Banana", "Orange"];
// Finding the index (position) of 'Banana'
var positionOfBanana = fruits.indexOf("Banana");
console.log("The Index of 'Banana' is:", positionOfBanana);
// Output (a) || The Index of 'Banana' is: 1
// Replacing the 'Banana' with 'Mango'
fruits[1] = "Mango";
console.log("The new array with 'Mango' is:", fruits);
// Output (a) || The new array with 'Mango' is: [ 'Apple', 'Mango', 'Orange' ]
// Removing the 'Orange' from the last of the array using 'Array pop() Method'
var removeOrange = fruits.pop();
console.log("The new array without 'Orange' is:", fruits);
// Output (b) || The new array without 'Orange' is: [ 'Apple', 'Mango' ]
// Adding the 'Watermelon' to the last of the array using 'Array push() Method'
var addWatermelon = fruits.push("Watermelon");
console.log("The new array with 'Watermelon' is:", fruits);
// Output (b) || The new array with 'Watermelon' is: [ 'Apple', 'Mango', 'Watermelon' ]