dakiddj25 / arrays_assignment

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

git# Arrays Lab

To complete this lab (as for most labs), do the following:

  1. Fork this repo.
  2. Clone your forked repo.
  3. Create a file called arrays_lab.js inside your cloned repo.
  4. Copy the description of the exercise as a comment.
  5. Underneath the commented description write your code.
  6. Ensure your solutions work properly!
  7. Open PR against this repo.

Exercises

1. Problem One - Fruits array

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 '$'.

2. Problem Two - Larger array

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]

3. Problem Three - Middle element

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

4. Problem Four - Iterate over an array

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

5. Problem Five - Sum of 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

6. Problem Six - Log odd/even numbers

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

7. Problem Seven - Smallest value

Given an array of numbers, write code that logs the smallest value

let arr = [4,3,7,29,40]

// 3

8. Problem Eight - Second smallest value

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

9. Problem Nine - Duplicates

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]

About


Languages

Language:JavaScript 100.0%