-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrays.js
More file actions
122 lines (101 loc) · 3.67 KB
/
Arrays.js
File metadata and controls
122 lines (101 loc) · 3.67 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//------------Array-------------
let marks = [97,98,99,85,85,86];
console.log(marks);
console.log(marks.length); //Property
let heros = ["iron man", "Hulk", "Thor", "Spidermnan"];
console.log(heros);
console.log(typeof(heros));
console.log(marks[0]);//index of Array
marks[0] = 100;
console.log(marks[0]);//index of Array is mutable but String is immutable
// Loop over Array
heros = ["iron man", "Hulk", "Thor", "Spidermnan","Spider","Batman","Flash"];
// for loop
for (let i = 0; i < heros.length; i++) {
console.log(heros[i]);
}
//for of loop
for (const hero of heros) {
console.log(hero);
}
/*
Let's Practice
Qs.For a given array with marks of students -> [85, 97, 44, 37, 76, 60]
Find the average marks of the entire class.
*/
let Marks= [85, 97, 44, 37, 76, 60];
let totalMarks = 0;
for (const marks of Marks) {
totalMarks = totalMarks + marks;
}
console.log("Total marks is "+totalMarks);
let average = totalMarks / Marks.length;
console.log(`Avarge marks is ${average}`);
/*
Qs. For a given array with prices of 5 items -> [250, 645, 300, 900, 50]
All items have an offer of 10% OFF on them. Change the array to store final price after applying offer.
*/
let prices = [250, 645, 300, 900, 50];
// for (let price of prices) { // it create new value do nothing with original value so should be used for loop instead
// price = price*90/100;
// }
for (let i = 0; i < prices.length; i++) {
prices[i] = prices[i]*90/100;
}
console.log(prices);
// Array Methods
let arrayMethods = [0, 1, 2, 3, 4, 5]
arrayMethods.push(6); // Added value at end of array
console.log(arrayMethods);
arrayMethods.unshift(-1); // Added value at start of array
console.log(arrayMethods);
arrayMethods.pop(); // deleted value at end of array
console.log(arrayMethods);
arrayMethods.shift(); // deleted value at start of array
console.log(arrayMethods);
console.log(arrayMethods.toLocaleString()); // to change array to string but doesnt change original array
console.log(arrayMethods);
let marvelHeroes = ["thor", "spiderman", "ironman"];
let dcHeroes = ["superman", "batman"];
let indianHeroes = ["shaktiman", "krish"];
let heroes = marvelHeroes.concat(dcHeroes, indianHeroes);//concat arrays but doesnt change original
console. log (heroes);
let newHeros = heroes.slice(0,3); // slice(start index, end index(non exclusive)) returns new array but doesn't change original
console. log (heroes);
console. log (newHeros);
//Splice method use add,delete and replace splice(start index, Number of delete items , replace value) ### IT WILL CHANGE THE ORIGINAL ARRAY ###
let arr = [1, 2, 3, 4, 5, 6, 71];
arr.splice(2, 2, 101, 102);
console.log(arr);
//Add Element
arr.splice(2, 0, 101);
console.log(arr);
//Delete Element
arr.splice(3, 1);
console.log(arr);
//Replace Element
arr.splice(3, 1, 103);
console.log(arr);
//Replace Element
arr.splice(3, 1, 103);
console.log(arr);
//cut the array from index
arr.splice(4); // [5 , 6 , 71]
console.log(arr);// [1, 2, 101, 103]
/*
Let's Practice
Qs. Create an array to store companies -> "Bloomberg", "Microsoft", "Uber", "Google", "IBM", "Netflix"
a. Remove the first company from the array
b. Remove Uber & Add Ola in its place
c. Add Amazon at the end
*/
let companies = ["Bloomberg", "Microsoft", "Uber", "Google", "IBM", "Netflix"];
//a
companies.shift();
console.log(companies);
//b
companies.splice(3,1,"Ola")
console.log(companies);
//c
companies.push("Amazon");
console.log(companies);