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...else
If a code block should be executed if an
if
condition
is not fulfilled, an else
is added.
let message;
if (amount > 1000) {
message = 'No payout possible!';
} else {
message = 'The amount will be paid out!';
}
Depending on whether amount
is greater or smaller 1000
,
either the if
branch or the else
branch is executed.Exercise
Write a function
Example:
addWithSurcharge
that adds two amounts with surcharge.
For each amount less than or equal to 10
, the surcharge is 1
.
For each amount greater than 10
, the surcharge is 2
.Example:
addWithSurcharge(5, 15)
should return 23
.
+ Hint
function addWithSurcharge(a, b) {
let surcharge = 0;
if (a <= 10) {
surcharge = surcharge + 1;
} else ...
...
return a + b + surcharge;
}
+ Solution
function addWithSurcharge(a, b) {
let surcharge = 0;
if (a <= 10) {
surcharge = surcharge + 1;
} else {
surcharge = surcharge + 2;
}
if (b <= 10) {
surcharge = surcharge + 1;
} else {
surcharge = surcharge + 2;
}
return a + b + surcharge;
}