The browser storage localStorage is not available. Either your browser does not support it or you have disabled it or the maximum memory size is exceeded. Without localStorage your solutions will not be stored.
if
Often code should only be executed if a certain condition is true.
To do this, use the
if
statement.
let win = 0;
if (dice === 6) {
win = 100;
}
This statement consists of the keyword if
followed by parentheses.
The parentheses contain an expression - the condition - that is evaluated to true
or false
.
If the condition results in true
, all statements in the block delimited by the curly brackets will be executed.
If the condition results in false
, the block bounded by the curly brackets will be skipped.
In our example, if dice
has the value 6
, then win
is set to 100
.
If dice
does not have the value 6
, then win
remains at 0
.Exercise
Write a function
Example:
equals
that checks two values for strict equality.
If the two values are equal, the string 'EQUAL'
should be returned.
If they are unequal, you should get 'UNEQUAL'
.Example:
equals(1, 1)
should return 'EQUAL'
and
equals(1, 2)
should return 'UNEQUAL'
.
+ Hint
function equals(a, b) {
// Initialize a variable with 'UNEQUAL'.
// Use 'if' to set the variable to 'EQUAL' if necessary.
// Return the variable.
}
+ Solution
function equals(a, b) {
let result = 'UNEQUAL';
if (a === b) {
result = 'EQUAL';
}
return result;
}