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.
Two returns
With
However, be careful using two or more return statements in a function. Such code can become obscure.
if
you can write functions with two return statements:
function prize(number) {
if (number === 6) {
return 100;
}
return 0;
}
If number
has the value 6
, the if
condition is fulfilled and the first return
statement will be executed. The function terminates and returns 100
. If number
does not have the
value 6
, the if
condition is not fulfilled. The code execution continues after the if
block.
The second return statement will be executed. The function terminates and returns 0
.However, be careful using two or more return statements in a function. Such code can become obscure.
Exercise
Write a function
Example:
repdigit
that determines whether a two-digit decimal is a
repdigit or not.
If the decimal is a repdigit, 'Repdigit!' should be returned, otherwise 'No Repdigit!'.Example:
repdigit(22)
should return 'Repdigit!'
and
repdigit(23)
should return 'No Repdigit!'
.
+ Hint
function repdigit(n) {
// Calculate the ones digit
// of n with modulo 10.
// Calculate the tens digit
// of n by dividing by 10
// and rounding down.
// Compare ones and tens digits.
}
+ Solution
function repdigit(n) {
let ones = n % 10;
let tens = Math.floor(n / 10);
if (ones === tens) {
return 'Repdigit!';
}
return 'No Repdigit!';
}