Javascript - Arrays 2

  • If you did not finish exercises 1-6 from Arrays 1, please work those problems first

Foreach

These exercises requires the foreach method:

https://www.freecodecamp.org/news/javascript-foreach-how-to-loop-through-an-array-in-js/

Exercise 1

  • Write an array with 3 strings

  • Using foreach - print each string

Exercise 2

  • Write an array with 5 numbers

  • Using foreach - write a program that sums the numbers

Exercise 3

  • Write an array with 5 numbers

  • Using foreach - write a program that calculates the average of the numbers

Filter method

These exercises requires the filter method

Linkarrow-up-right

Exercise 1

  • Write an array with 5 numbers - some larger than zero some smaller

  • Using the filter function of the array

    • create a new array with all values larger than 0

  • Print and verify the result

Exercise 2

  • Write an array with 5 strings

  • Using the filter function of the array:

    • Create a new array with all strings having a length of 5 or higher

  • Print and verify the result

Exercise 3

  • Write an array with 5 strings

  • Write a program that adds every string to a new array

    • Except if the string has more than 3 characters

  • Print & verify the result

Exercise 4

  • Write an exercise for new students to use to better understand arrays

  • Create the problem statement and write the code to solve it

  • Think: What exercise could make you understand arrays better?

Last updated