54ccff4537af96c8f3eef62be2e76272
linux
node1
v1
rasb14
2019-02-10 21:58:08
v3.1.3 (2018-09-13)

Generated 2019-02-10 22:58:08 by dbwebb lab-utility v3.1.3 (2018-09-13).


node1 - JavaScript med Nodejs

JavaScript using nodejs.

1. nodejs built-ins

In this section we try out some of the new nodejs and ES6 features.

Exercise 1.1 (1 points)

Create a variable called numbersArray holding the numbers 6,46,12,19,82.

Use find to get the first occurence of a number bigger than or equal to 42.

Answer with the number.

Exercise 1.2 (1 points)

Find the smallest number in numbersArray by using the spread operator ... and the function Math.min().

Answer with the smallest number.

Exercise 1.3 (1 points)

Create a function called meaningOfLife() with one default parameter with the value of 42.

The function should return the sentence ‘The meaning of life is ’ concatenated with the parameter.

Answer with a call to the meaningOfLife() function without any parameters.

Exercise 1.4 (1 points)

Check if the word Kangaroo contains the letters ‘oo’. Return true or false depending on the answer.

Tip: Use nodejs function includes.

Exercise 1.5 (1 points)

Check if the word Kangaroo starts with the letters ‘El’. Return true or false depending on the answer.

Tip: Use nodejs function startsWith.

2. Filtering arrays

In this section we filter arrays in different ways.

Exercise 2.1 (1 points)

Use numbersArray from above holding the numbers 6,46,12,19,82.

Use a for-loop to save all numbers smaller than 42 in a new array.

Answer with the resulting array.

Exercise 2.2 (1 points)

Create a variable called moreNumbersArray holding the numbers 6,46,12,19,82,66,36.

Use the built-in higher-order function filter and a callback function to filter out all numbers bigger than or equal to 42.

Use arrow-notation to keep the code short and concise.

Answer with the resulting array.

3. Transforming arrays

In this section we change arrays using the higher-order functions map and reduce.

Exercise 3.1 (1 points)

Create a variable called stringArray holding the strings ‘Jim Lovell’,’Jack Swigert’,’Fred Haise’.

Use a for-loop to concatenate the string ’ was on the apollo 13’ too each name in the array.

Store the result in a new array and answer with that array.

Exercise 3.2 (1 points)

Use the stringArray from above and the built-in higher-order function map to concatenate the string ’ was not on the apollo 12’ and each name.

Use arrow notation to keep the code simple and concise.

Answer with the resulting array.

4. Extra assignments

Solve these optional questions to earn extra points.

Exercise 4.1 (3 points)

Create a variable called maybePrimeNumber holding the numbers 73,78,83,88,97,102,103,106,109.

In a for-loop sum all prime numbers from maybePrimeNumber, you need to find out whether or not the number is a prime number.

Answer with the resulting sum.

Exercise 4.2 (3 points)

Create a function isNotPrime() that takes one parameter (an integer) and tests if that number is a prime number. If the number is not prime, the number is returned otherwise return 0.

Create the variable maybePrimeNumber once more with the numbers 73,78,83,88,97,102,103,106,109. Use the built-in higher-order function reduce to sum all numbers in maybePrimeNumber that are NOT prime numbers.

Answer with the resulting sum.


54ccff4537af96c8f3eef62be2e76272