-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhigher.js
More file actions
38 lines (26 loc) · 956 Bytes
/
higher.js
File metadata and controls
38 lines (26 loc) · 956 Bytes
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
//Arrays
const students = ["Piyush", "john", "jane"];
//Array foreach method
students.forEach((val) => console.log(val));
//map method that return a new array
const numbers = [0, 1, 2, 3, 40, 4, 6];
console.log(numbers.map((val) => val * 2));
//return number if number exist
console.log(numbers.find((n) => n == 4));
//return index if number exist
console.log(numbers.findIndex((n) => n == 4));
//return boolean
console.log(numbers.includes(40));
//return array providing indicess or index
console.log(numbers.slice(2, 4));
//challanges
//1. sort the array in ascending order
numbers.sort((a, b) => a - b); //doing b-a will sort in decending order
console.log(numbers);
//2. filter out the numbers which are divisible by 2
console.log(numbers.filter((n) => n % 2 == 0));
//3. reduce the array to single value
let sum = numbers.reduce(
(accumulator, currentValue) => accumulator + currentValue
);
console.log("sum of all elements is: ", sum);