To complete this lab (as for most labs), do the following:
- Fork this repo.
- Clone your forked repo.
- Create a file called
arrays_lab.js
inside your cloned repo. - Copy the description of the exercise as a comment.
- Underneath the commented description write your code.
- Ensure your solutions work properly!
- Open PR against this repo.
a. Add "mango" to the end of a fruits array ["strawberry", "banana"].
b. Add "blueberry" to the front of the same fruits array.
c. Remove the last element of the fruits array.
d. console.log the length of the fruits array.
e. Remove the first element of the fruits array.
f. Join all the elements in the fruits array with a '$'.
Given two arrays, write code that logs the larger array. If the arrays are the same length, log "They are the same size"
let firstArr = [1,2,3]
let secondArr = [1,5,2,4]
// log [1,5,2,4]
Given an array, log its middle element. If the middle of the array is between two elements, log both of them.
let hasExactMiddleArr = [1,5,3]
// logs 5
let hasNoExactMiddleArr = [1,4,6,9]
// logs 4,6
a. Write code that logs every value in an array using a for
loop
let logMeOutFor = ["I", "am", "a", "happy", "array"]
/* I
am
a
happy
array
*/
b. Write code that logs every value in an array using a while
loop
let logMeOutWhile = ["I", "am", "a", "happy", "array"]
/* I
am
a
happy
array
*/
c. Write code that logs every value in an array using a for/of loop
let logMeOutForOf = ["I", "am", "a", "happy", "array"]
/* I
am
a
happy
array
*/
Given an array of numbers, write code that logs the sum of all the numbers
let firstArrToSum = [1, 2, 3, 4, 5, 6]
// 21
let secondArrToSum = [1, 2, 3, 4, 5, -6]
// 9
a. Given an array of numbers, write code that logs all the odd numbers
let evenAndOddArr = [1,5,2,4,11,12,99,100]
// 1, 5, 11, 99
b. Given an array of numbers, write code that logs the sum of all the even numbers
let evenAndOddArrToSum = [1,5,2,3,11,4,6]
// 12
Given an array of numbers, write code that logs the smallest value
let arr = [4,3,7,29,40]
// 3
Find the second smallest number in an Array of Ints
let secondSmallestArr = [11, 52, 10, 7, 50, 46, 79, 78, 13, 26, 83, 92, 89, 81, 1, 41, 4, 23, 57, 41, 80, 83, 41, 69]
// 4
Write code such that noDupeList has all the same numbers as dupeFriendlyList, but has no more than one of each number.
Hint
Make another array to store all the values you've seen so far. When looking at a new value, see if your array includes the value, and only add it to the noDupeList
if it doesn't.
let dupeFriendlyList = [4,2,6,2,2,6,4,9,2,1]
let noDupeList = []
// noDupleList = [4, 2, 6, 9, 1]