I am using the function from developer.mozilla.com
console.log(getRandomInt(1));
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
I need it to only be 1 or 0.
If I call the above script in the chrome developer console, then I always get "undefined"
The reason behind this behavior is Math.floor()
. From the documentation:
The Math.floor() function returns the largest integer less than or equal to a given number.
Source: Math.floor()
What you need to use to make it work as per your requirement is Math.round()
. As the documentation states:
The Math.round() function returns the value of a number rounded to the nearest integer.
Source: Math.round()
Please find this example below:
console.log(getRandomInt(1));
function getRandomInt(max) {
const random = Math.random() * max;
return Math.round(random);
}
Hope this helps!
Addition:
And as @Kaiido
mentioned you have an active filter with the value name
. If you remove that you will see the value.