644079eb300f789ad12d5da2793642f6
javascript1
lab2
v2
rasb14
2018-11-19 16:07:02
v3.1.3 (2018-09-13)

Generated 2018-11-19 17:07:02 by dbwebb lab-utility v3.1.3 (2018-09-13).


Lab 2 - statements

Statements and expressions in JavaScript.

Use MDN as your reference and base to solving the exercises.

1. If, else if and else

Exercise 1.1 (1 points)

Create five variables: card1, card2, card3, card4, card5.

Assign the values 2, 7, 3, 10, 4 to the variables created above.

Add them up and put the sum in a variable called result.

Answer with the variable result.

Exercise 1.2 (1 points)

Use an if statement to see if the five cards (card1-card5) have a combined sum that is higher than 21.

Create a variable status and give it a different value depending on the following.

Answer with your status-variable.

Exercise 1.3 (1 points)

Use if else statements to see if the combined value of the first three cards (card1-card3) is lower, higher or exactly 21.

Answer with a string corresponding to the result:
lower = "safe"
higher = "busted"
21 = "black jack"

Store the response in your status-variable and answer with it.

Exercise 1.4 (2 points)

Create three variables: dealer1, dealer2, dealer3.

Assign the values 11, 5, 4 to the variables.

Combine the if, else if, else statements and the operator AND (&&) to see what the dealer should do. Combine as you feel needed.

If the sum of the dealercards is lower than 17, answer with "pick", if the sum is higher than or equal to 17 and lower than 21 answer with "stop". If the sum is 21 answer with "black jack". If the sum is higher than 21 answer with "busted".

Store the response in a variable, and answer with it.

PS. You can change the sum to test that your if-statement really works for all values, just to try it out.

2. Switch, case

Exercise 2.1 (1 points)

Use a switch-case statement to create a message with the type of fruit and its color. You have the following type of fruits with a corresponding color:

Create a variable myFruit which holds the current type of your fruit. If ‘myFruit’ is banana, the result should be a variable containing the string value "The banana is yellow."

Ensure that your switch-case works for all values.

Answer with the result where myFruit = "plum".

Exercise 2.2 (1 points)

Extend your switch-case statement with a default value. The result should be:

"That is an unknown fruit." when the variable ‘myFruit’ has an unknown value.

Answer with the result where ‘myFruit = pear’.

3. For loops

Exercise 3.1 (1 points)

Use a for-loop to increment 713 with the value 3, 13 times.

Answer with the result.

Exercise 3.2 (1 points)

Use a for-loop to decrement 988 with the value 9, 18 times.

Answer with the result.

Exercise 3.3 (3 points)

Use a for-loop to add all the even values in the range 21 to 49 to a string with each number separated by a comma ,.

The result should not end with a comma. You should neither have a space after the comma.

Answer with the resulting string.

4. While loops

Exercise 4.1 (1 points)

Use a while-loop to increment 13 with the value 3 until it has reached or passed 713.

Answer with the amount of steps needed.

Exercise 4.2 (1 points)

Use a while-loop to subtract 9 from 988 until the value has reached or passed 0.

Answer with the amount of steps needed.

Exercise 4.3 (3 points)

Use a while-loop to add all the values, to a comma-separated string, in the range 23 to 64, where the value is divisable by 5 or 7.

Answer with the resulting string.


644079eb300f789ad12d5da2793642f6