Last active
February 21, 2019 00:10
-
-
Save camckin10/7fcd07d4402f895cfee23167d6e15f25 to your computer and use it in GitHub Desktop.
arraydrills.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1.) Creating arrays | |
//function with 3 arguments, item1,item2,item3 | |
//return three items as an array | |
function makeList(item1,item2,item3) { | |
return [item1,item2,item3]; | |
} | |
2.) Adding array items | |
//function called addToList that accepts an array and an item as arguments, and returns the | |
//array with the item added to the end. Function will have side effect of changing original array. | |
function addToList (list, item) { | |
list.push(item) | |
return list | |
} | |
3.) Accessing array items | |
//2 functions: accessFirstItem(array), and accessThirdItem(array) | |
//Both functions take single argument, an array of values called array | |
//accessFirstItem returm 1st item in array | |
//acccessThirdItem return 3rd item in array | |
function accessFirstItem(array){ | |
var items = array[0]; | |
return array[0]; | |
} | |
function acccessThirdItem(array){ | |
var items = array[2]; | |
return array[2]; | |
} | |
4.) Array length and access | |
//2 functions: findLength and accessLastItem | |
//Both functions take 1 argument: an array of values | |
//findLength return length of array | |
//accessLastItem return last value in array without changing array | |
function findLength(array) { | |
return array.length | |
} | |
function accessLastItem (array) { | |
var items = array[4]; | |
return array[4]; | |
} | |
5.) Array copying I | |
//2 functions firstFourItems, and lastThreeItems | |
//both functions take a single argument: an array of values called array (Includes at least 5 values, maybe more) | |
//firsrFourItems print first four items in array | |
//lastThreeItems print last three items in array | |
function firstFourItems(array){ | |
return array.slice(0); | |
} | |
function lastFourItems(array){ | |
return array.slice(-3); | |
} | |
6.) Array copying II | |
//2 functions, minusLastItem, and copyFirstHalf | |
//1 argument, an array of values called array | |
//minusLastItem, copy entire array except last item | |
//copyFirstHalf, return array that copies first half | |
//copyFirstHalf should return smaller half if there are uneven numbers | |
function minusLastItem(array){ | |
return array.slice(0,array.length)); | |
} | |
function copyFirstHalf(array){ | |
return array.slice(0,array.length/2)); | |
} | |
//question:why use .slice method? | |
7.) Squares with map | |
//function named squares | |
//takes 1 argument, array of values called array | |
//function should return a new array with the square value from the original array | |
//no loop, MUST use .map method | |
function squares(array) { | |
var numbers= array | |
var roots= numbers.map(function(x){ | |
return x*2; | |
}); | |
function squares(array){ | |
var doubles.array.map(function(num){ | |
return num**2; | |
}); | |
return doubles; | |
} | |
8.) Sort | |
//function greatestToLeast | |
//takes 1 argument:array | |
//function return array with sorted numbers greatest to least | |
function greatestToLeast(array){ | |
return array.sort(function(a,b){ | |
return b-a; | |
}); | |
} | |
9.) Filter | |
//function shortWords | |
//1 argument:an array of strings called array | |
//function should return an array with strings that are shorter than 5 characters | |
//use .filter method | |
function shortWords(array){ | |
return array <= 6; | |
var filtered = array.filter(shortWords); | |
return function shortWords | |
function shortWords(array){ | |
return array.filter(function(word){ | |
return word.length<5; | |
}); | |
} | |
10.) Find | |
//function divisibleBy5 | |
//1 argument, array of numbers called array | |
//function should return 1st value in array that is divisible by 5. Not divisible by 5, return undefined | |
//Use .find method | |
//*COULD ALSO POSSIBLY USE FOR LOOP TO SOLVE PROBLEM* | |
function divisibleBy5(array){ | |
return array.find(function(num){ | |
return num % 5 == 0; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment