|
26 | 26 | // Array sort => Alphabetically, |
27 | 27 | // doesn't sort numbers |
28 | 28 | // This sort method mutates the original array |
29 | | -const names = ["Shubham", "Aditya", "Divyanshi", "Samarth"]; |
30 | | -names.sort(); |
31 | | -console.log(names); |
| 29 | +// const names = ["Shubham", "Aditya", "Divyanshi", "Samarth"]; |
| 30 | +// names.sort(); |
| 31 | +// console.log(names); |
32 | 32 |
|
33 | | -const numbers = [4, 12, 8, 5, 1]; |
| 33 | +// const numbers = [4, 12, 8, 5, 1]; |
34 | 34 |
|
35 | 35 | // Ascending order |
36 | | -numbers.sort((a, b) => a - b); |
37 | | -console.log(numbers); |
| 36 | +// numbers.sort((a, b) => a - b); |
| 37 | +// console.log(numbers); |
38 | 38 |
|
39 | 39 | // Descending order |
40 | | -numbers.sort((a, b) => b - a); |
41 | | -console.log(numbers); |
| 40 | +// numbers.sort((a, b) => b - a); |
| 41 | +// console.log(numbers); |
| 42 | + |
| 43 | +// const array = [1, 2, 3, 4, 5]; |
| 44 | + |
| 45 | +// Array Some => returns true if atleast one element passes the test |
| 46 | +// console.log(array.some((number) => number > 5)); // false |
| 47 | + |
| 48 | +// Array Every => return true if all elements pass the test |
| 49 | +// console.log(array.every((number) => number > 0)); // true |
| 50 | + |
| 51 | +// Array Reduce |
| 52 | + |
| 53 | +const groceryList = [29, 12, 45, 35, 87, 110]; |
| 54 | + |
| 55 | +const total = groceryList.reduce((total, price) => total + price, 0); |
| 56 | + |
| 57 | +console.log(total); // 318 |
0 commit comments