Skip to content

Commit 66364f8

Browse files
committed
day 4 js Array
1 parent 255ea2d commit 66364f8

3 files changed

Lines changed: 111 additions & 0 deletions

File tree

Array/index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
10+
</body>
11+
<script src="scripts.js"></script>
12+
</html>

Array/scripts.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//Array
2+
/*
3+
Collection Of items
4+
*/
5+
let marks=[27,56,98,65,87,60];
6+
let sum=0;
7+
for(let val of marks){
8+
sum=sum+val;
9+
}
10+
let avg=sum/marks.length;
11+
console.log(`Sum of the marks=${sum}`);
12+
console.log(`Average of marks=${avg}`);
13+
14+
//Arrays in loops
15+
let cities=["Delhi","Kolkata","Pune","Bangalore"];
16+
for (let city of cities){
17+
console.log(city)
18+
}
19+
20+
21+
//Q2
22+
let items=[65,98,78,63,21,56];
23+
for(let i=0;i<items.length;i++){
24+
let offer=items[i]/10;
25+
items[i]-=offer;
26+
}
27+
console.log(items);
28+
29+
30+
//push this method add the items in last index
31+
let fooditems=["potato","apple","litchi"];
32+
console.log("Before Push =",fooditems);
33+
fooditems.push("chips");
34+
console.log(" After push Food items are =",fooditems);
35+
36+
//pop this method delete from end
37+
let fitems=["potato","apple","litchi"];
38+
console.log("Before pop =",fitems);
39+
let del=fitems.pop();
40+
console.log(" After pop Food items are =",fitems);
41+
console.log("Deleted item is=",del)
42+
43+
44+
//ToString coverts the array to string
45+
let cgpa=[8,9,7,8.3,7.5];
46+
console.log("Array is=",cgpa);
47+
console.log("Converted into String=",cgpa.toString());
48+
49+
50+
//concat Array
51+
let marvel=["Thor","Ironman","Spiderman"];
52+
let dc=["Batman","Superman","Wonder Woman"];
53+
console.log("marvel=",marvel);
54+
console.log("dc=",dc);
55+
let hero=marvel.concat(dc);
56+
console.log("After Concat =",hero);
57+
58+
//unshift add to start
59+
//shift delete from start
60+
let ab=[1,2,3,5];
61+
console.log("Before unshift",ab);
62+
let b=ab.unshift(9);
63+
console.log("After unshift",ab);
64+
let c=ab.shift();
65+
console.log("After Shift=",ab);
66+
67+
68+
//slice
69+
let bc=[1,2,3,4,5];
70+
console.log("Array=",bc);
71+
console.log(bc.slice(1,2));
72+
73+
//splice add delete replace the element
74+
let arr=[5,6,9,8,3];
75+
console.log("Array is",arr);
76+
arr.splice(2,0,10);// add 2 is index 0 deleted item 10 inserted item
77+
console.log("after Insertion=",arr);
78+
arr.splice(3,1)//delete 3 is index no 1 number of deletion
79+
console.log("After Deletion=",arr);
80+
arr.splice(4,1,11);//replace 4 index no 1 number of deletion 11 inserted instead of
81+
console.log("After Replace",arr);
82+
83+
//Practice 1
84+
let company=["Bloomberg","Microsoft","Uber","Google","IBM","Netflix"];
85+
console.log("Companies",company);
86+
87+
//Remove first Company From Array
88+
89+
console.log("Delete the First Company=",company.shift());//delete from start
90+
console.log("After deletion",company);
91+
92+
//Remove Uber & add Ola (replace)
93+
94+
company.splice(1,1,"Ola") // 1 is index no 1 deleted "Ola" is added
95+
console.log("After Replacement of Uber added item is Ola",company);
96+
97+
//Add "Amazon" at the end
98+
company.push("Amazon");
99+
console.log("After Pushing at the end",company);

Chapter 4 Array.docx

669 KB
Binary file not shown.

0 commit comments

Comments
 (0)