1+ // Ch 5 Array Practice Set
2+
3+
4+
5+ // Q1)How do you create an empty array in JavaScript?
6+
7+ // Answer:
8+
9+ // const emptyArray = [];
10+
11+
12+ // Q2)How do you check if an array is empty in JavaScript?
13+
14+ // Answer:
15+
16+ // const array = [];
17+
18+ // if (array.length === 0) {
19+ // console.log("The array is empty");
20+ // } else {
21+ // console.log("The array is not empty");
22+ // }
23+
24+
25+ // Q3)How do you add elements to the end of an array in JavaScript?
26+
27+ // Answer:
28+
29+ // const array = [1, 2, 3];
30+ // array.push(4, 5);
31+ // console.log(array); // Output: [1, 2, 3, 4, 5]
32+
33+
34+ // Q4)How do you access an element at a specific index in an array?
35+
36+ // Answer:
37+
38+ // const array = [1, 2, 3];
39+ // const element = array[1];
40+ // console.log(element); // Output: 2
41+
42+
43+ // Q5)How do you remove the last element from an array in JavaScript?
44+
45+ // Answer:
46+
47+ // const array = [1, 2, 3];
48+ // const removedElement = array.pop();
49+ // console.log(array); // Output: [1, 2]
50+ // console.log(removedElement); // Output: 3
51+
52+
53+ // Q6)How do you find the index of a specific element in an array?
54+
55+ // Answer:
56+
57+ // const array = [1, 2, 3, 4, 5];
58+ // const index = array.indexOf(3);
59+ // console.log(index); // Output: 2
60+
61+
62+ // Q7)How do you concatenate two arrays in JavaScript?
63+
64+ // Answer:
65+
66+ // const array1 = [1, 2, 3];
67+ // const array2 = [4, 5, 6];
68+ // const concatenatedArray = array1.concat(array2);
69+ // console.log(concatenatedArray); // Output: [1, 2, 3, 4, 5, 6]
70+
71+
72+ // Q8)How do you check if an element exists in an array?
73+
74+ // Answer:
75+
76+ // const array = [1, 2, 3, 4, 5];
77+ // const elementExists = array.includes(3);
78+ // console.log(elementExists); // Output: true
79+
80+
81+ // Q9)How do you find the maximum value in an array?
82+
83+ // Answer:
84+
85+ // const array = [4, 2, 7, 5, 1];
86+ // const max = Math.max(...array);
87+ // console.log(max); // Output: 7
88+
89+
90+ // Q10)How do you reverse the order of elements in an array?
91+
92+ // Answer:
93+
94+ // const array = [1, 2, 3, 4, 5];
95+ // array.reverse();
96+ // console.log(array); // Output: [5, 4, 3, 2, 1]
0 commit comments