Skip to content

Commit 62c35ae

Browse files
committed
Array Reduce
1 parent a529210 commit 62c35ae

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

7_Arrays_in_detail/Arrays.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,27 @@ numbers.sort((a, b) => b - a);
167167
console.log(numbers); // [ 12, 8, 5, 4, 1 ]
168168
```
169169

170+
#### Some and Every
171+
172+
```js
173+
const array = [1, 2, 3, 4, 5];
174+
175+
// Array Some => returns true if atleast one element passes the test
176+
console.log(array.some((number) => number > 5)); // false
177+
178+
// Array Every => return true if all elements pass the test
179+
console.log(array.every((number) => number > 0)); // true
180+
```
181+
182+
#### Array Reduce
183+
184+
```js
185+
// Array Reduce
186+
187+
const groceryList = [29, 12, 45, 35, 87, 110];
188+
189+
const total = groceryList.reduce((total, price) => total + price, 0);
190+
191+
console.log(total); // 318
192+
```
193+

7_Arrays_in_detail/Arrays_3.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,32 @@
2626
// Array sort => Alphabetically,
2727
// doesn't sort numbers
2828
// 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);
3232

33-
const numbers = [4, 12, 8, 5, 1];
33+
// const numbers = [4, 12, 8, 5, 1];
3434

3535
// Ascending order
36-
numbers.sort((a, b) => a - b);
37-
console.log(numbers);
36+
// numbers.sort((a, b) => a - b);
37+
// console.log(numbers);
3838

3939
// 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

Comments
 (0)