const generateRandomBetween = (min,max,exclude) => {
min=Math.ceil(min);
max =Math.floor(max);
const rndnum= Math.floor(Math.random()*(max-min)) + min;
if(rndnum===exclude)
{
generateRandomBetween = (min,max,exclude)
}
else
{
return rndnum;
}
"here this code is generating random number between 1-100 and exclude used for computer not guessing the number in first try" "
generateRandomBetween
in your case is a function that is assigned to a const
variable.
Variables defined as const
can not be reassigned, unlike let
or var
.
Hence, whenever the value of rndnum
is equal to the value of exclude
,
you are trying to assign a new value to generateRandomBetween
.
Im assuming that what you meant to say is that if rndnum
is equal to exclude
, re-run the function.
If that is the case, you should remove the "equals" sign.
Example:
const generateRandomBetween = (min,max,exclude) => {
min=Math.ceil(min);
max =Math.floor(max);
const rndnum= Math.floor(Math.random()*(max-min)) + min;
if(rndnum===exclude)
{
generateRandomBetween(min,max,exclude)
}
else
{
return rndnum;
}
}
Note: we can also make this code a bit cleaner if we do a small syntax refactor.
const generateRandomBetween = (min,max,exclude) => {
min = Math.ceil(min);
max = Math.floor(max);
const rndnum = Math.floor(Math.random()*(max-min)) + min;
if(rndnum === exclude) {
return generateRandomBetween(min,max,exclude);
}
return rndnum;
}
The functionality remains the same, we just make the else
block implicit, so to speak.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.