|
| 1 | +// Initial Data |
1 | 2 |
|
2 | 3 | let arr = [5, 3, 8, 1]; |
| 4 | +console.log("Original array of numbers: ",arr) |
3 | 5 |
|
4 | | -console.log("Filtering elements"); |
| 6 | +// 1 - if/else |
5 | 7 |
|
6 | 8 | function filterRange(arr,a, b) { |
| 9 | + console.log("Filtering elements with if-else statements:"); |
7 | 10 | return arr.filter(function(element) { |
8 | | - |
9 | | - if ((element >= a) && (element <= b)) { |
| 11 | + if ((element >= a) && (element <= b)) { |
10 | 12 | return true; |
11 | 13 | } else { |
12 | 14 | return false; |
13 | 15 | } |
14 | 16 | }); |
15 | | - |
16 | 17 | } |
17 | 18 | filtered=filterRange(arr, 1, 4); |
| 19 | +console.log(filtered); |
18 | 20 |
|
19 | | -console.log("Original array: ",arr) |
20 | | -console.log("Filtered array: ",filtered); |
21 | | - |
| 21 | +//2 - arrow func |
22 | 22 |
|
| 23 | +function filterRange2 (arr,a,b){ |
| 24 | + console.log("Filtering elements with arrow function:"); |
| 25 | + let filtered2 = arr.filter((num)=> (num>=a)&&(num<=b)) |
| 26 | + console.log(filtered2); |
| 27 | +} |
| 28 | +filterRange2(arr,1,4); |
0 commit comments