├── index.html └── app.js /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Document 8 | 9 | 10 |

Higher Oredr Array Methods

11 | 12 | 13 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | const numbers = [-1,-5,-32,0,23,44,22,88,123,777,54,98]; 2 | 3 | // Sort Array from lowest to largest 4 | const sortAscendingArray = arr=> arr.sort(function(a,b){ 5 | return a-b; 6 | }); 7 | 8 | console.log(sortAscendingArray(numbers)) 9 | 10 | // Sort array from largest to lowest 11 | const sortDescendingArray = arr => arr.sort(function(a,b){ 12 | return b - a; 13 | }) 14 | console.log(sortDescendingArray(numbers)) 15 | 16 | //Get array total 17 | const arrTotal = arr=> arr.reduce((num,currNum)=> num + currNum,0) 18 | 19 | console.log(arrTotal(numbers)) 20 | 21 | //Filter out unwanted values in an array (Negative numbers) 22 | const arrFilter = arr => arr.filter(num=>num >= 0); 23 | console.log(arrFilter(numbers)) 24 | 25 | // LOOP AN ARRAY 26 | //Using forEach method 27 | const arrLoop = arr => arr.forEach(function(num){ 28 | return console.log(num) 29 | }); 30 | console.log(arrLoop(numbers)) 31 | 32 | //using for...of method 33 | const arrLoop2 = arr =>{ 34 | for(let num of arr){ 35 | console.log(num) 36 | } 37 | } 38 | 39 | console.log(arrLoop2(numbers)) 40 | 41 | // Make a copy of an array without altering the original array 42 | const makeCopy = arr => arr.map(num => num + 1) 43 | console.log(makeCopy(numbers)) 44 | 45 | --------------------------------------------------------------------------------