Homework

JavaScript Iterators

.forEach() or .filter()

Write a function named onlyEven that returns evens numbers in an array.

onlyEven([1,2,3,4,5,6,7]) // => [2,4,6]

.forEach() or .filter()

  • countIntegers – counts how many integers there are in an array

countIntegers([4, 2.2, 5, 6, 4.2, 8.2, 4]) // => 4
  • Hint: Google how you can check if a number is an integer in JavaScript

.map() oldie but goodie

Write a function lengths that accepts a single parameter as an argument, namely an array of strings. The function should return an array of numbers where each number is the length of the corresponding string. use .map

lengths(['my', 'cake', 'pudding']); // => [2,4,7]

.map()

Write code using .map() to have the following input and output:

  • getSquares – takes in an array of numbers and returns an array of their squares

getSquares([1, 2, 3, 4, 5]) // => [1, 4, 9, 16, 25]

.map(), .filter() & .reduce()

  1. Write an expression using array iterator methods (like filter and reduce) to compute the total value of the machines in the inventory array.

var inventory = [
  {type:   "machine", value: 5000},
  {type:   "machine", value:  650},
  {type:      "duck", value:   10},
  {type: "furniture", value: 1200},
  {type:   "machine", value:   77}
]
  1. Get an array of all the machines with value over 500

Last updated