Condition overlaps?

if (randomNum >= 0 && randomNum <= 1 / 3) {
compMove = 'Rock';
}
else if (randomNum > 1 / 3 && randomNum <= 2 / 3) {
compMove = 'Paper';
}
else {
compMove = 'Scissors';
}
if (randomNum >= 0 && randomNum <= 1 / 3) {
compMove = 'Rock';
}
else if (randomNum > 1 / 3 && randomNum <= 2 / 3) {
compMove = 'Paper';
}
else {
compMove = 'Scissors';
}
Can anyone confirm this as the above code will not cause any overlap in value
54 Replies
Vandana
VandanaOP3mo ago
No overlap or gaps occur? and every value in the range from 0 to 1 is accounted for. Am i good to go?
glutonium
glutonium3mo ago
instead of going into floating points, a better approach is to have 3 numbers 0,1,2 where 0 = rock 1 = paper 2 = scissors so basically u cab randomly generate an int between 0 to 2 and then based on which number is picked u make the corresponding move
Vandana
VandanaOP3mo ago
So this may result in errors ?
ἔρως
ἔρως3mo ago
how about this how do you handle the player input? yes, because 1/3 is not the same as 0.1 + 0.2 and is not the same as 0.3.
Vandana
VandanaOP3mo ago
yea here we are taking the entire value of 1/3 right 0.33333333333 we dont require any round off result here as in case of calcuating money , its pretty accurate result (floating value we are comparing 1/3= 0.33333333333).
ἔρως
ἔρως3mo ago
you're right, but 0.3333... isn't 0.3333.... 1/3 is actually 0.3333333730697632
glutonium
glutonium3mo ago
knew that was coming
ἔρως
ἔρως3mo ago
it is an automatic fail there's ~0.00000004 of bias towards an outcome and 0.666666 has a bias too: 0.666666686534881591796875 that's another fail too
ἔρως
ἔρως3mo ago
https://stackoverflow.com/a/42321673 <-- i found this to generate a single cryptographically secure random number
Stack Overflow
Javascript: Generate a random number within a range using crypto.ge...
I understand you can generate a random number in JavaScript within a range using this function: function getRandomInt (min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; }
ἔρως
ἔρως3mo ago
it's overkill, but unbiased
Vandana
VandanaOP3mo ago
This is so frustrating then why in the console it gives 1/3 as 0.3333333333
ἔρως
ἔρως3mo ago
because javascript is tricking you it's hidding the error part from you
Vandana
VandanaOP3mo ago
how can i scan when its hiding ?when its not hiding
ἔρως
ἔρως3mo ago
no idea ¯\_(ツ)_/¯ always assume all floats are wrong
Vandana
VandanaOP3mo ago
then this gives error too while cabing
ἔρως
ἔρως3mo ago
my answer depends on the rest of your code can you show it?
Vandana
VandanaOP3mo ago
<button onclick="playGame('Rock')">Rock</button>
<button>Paper</button>
<button>Scissors</button>
<script>
function playGame(playerMove) {
let compMove;
let randomNum = Math.random();
if (randomNum >= 0 && randomNum <= 1 / 3) {
compMove = 'Rock';
}
else if (randomNum > 1 / 3 && randomNum <= 2 / 3) {
compMove = 'Paper';

}
else {
compMove = 'Scissors';
}

}

</script>
<button onclick="playGame('Rock')">Rock</button>
<button>Paper</button>
<button>Scissors</button>
<script>
function playGame(playerMove) {
let compMove;
let randomNum = Math.random();
if (randomNum >= 0 && randomNum <= 1 / 3) {
compMove = 'Rock';
}
else if (randomNum > 1 / 3 && randomNum <= 2 / 3) {
compMove = 'Paper';

}
else {
compMove = 'Scissors';
}

}

</script>
ἔρως
ἔρως3mo ago
so, you don't even have a way for the player to pick an option?
Vandana
VandanaOP3mo ago
I was using 1/3 actually I am still working on it
ἔρως
ἔρως3mo ago
you've started """"wrong"""" you don't have anywhere that has the possible values
Vandana
VandanaOP3mo ago
Among three buttons he will click one which he wants either rock or paper or scissor which values?
ἔρως
ἔρως3mo ago
well, the valid inputs something like this:
const moves = {
rock: {
icon: '🪨',
title: 'Rock',
},
paper: {
icon: '📜',
title: 'Paper',
},
scissors: {
icon: '✂️',
title: 'Scissors',
},
};
const moves = {
rock: {
icon: '🪨',
title: 'Rock',
},
paper: {
icon: '📜',
title: 'Paper',
},
scissors: {
icon: '✂️',
title: 'Scissors',
},
};
and then, you can just select a random key, for the enemy
Vandana
VandanaOP3mo ago
yes this is the further modification
ἔρως
ἔρως3mo ago
it should be the first step you have to know the valid inputs before you write anything planning is extremely important
Vandana
VandanaOP3mo ago
ohh <button onclick="playGame('Rock')">Rock</button> <button>Paper</button> <button>Scissors</button> i have planned na
ἔρως
ἔρως3mo ago
it's 2024, there's almost no place for onclick also, here's a much better way to do it:
<form action="javascript:void(0)">
<button type="submit" value="rock">Rock</button>
<button type="submit" value="paper">Paper</button>
<button type="submit" value="scissors">Scissors</button>
</form>
<form action="javascript:void(0)">
<button type="submit" value="rock">Rock</button>
<button type="submit" value="paper">Paper</button>
<button type="submit" value="scissors">Scissors</button>
</form>
Vandana
VandanaOP3mo ago
YES As the first step right?
ἔρως
ἔρως3mo ago
yes, as part of the ui
Vandana
VandanaOP3mo ago
instead of onClick we can use event listeners.
ἔρως
ἔρως3mo ago
instead of 3 onclicks, you can have 1 onsubmit on the form
Vandana
VandanaOP3mo ago
This 1 onSubmit gives any value right either rock,paper,or scissor
ἔρως
ἔρως3mo ago
yes, through event.submitter which contains the button that submitted the form and buttons can have html, which means that you can add an image and text too, to make it pretty or use some html to make the emoji big
Vandana
VandanaOP3mo ago
yes getting my main doubt is about the float condition comparision What can i do to avoid the float issues
ἔρως
ἔρως3mo ago
nothing, throw away the code
Vandana
VandanaOP3mo ago
this is a way to pick option for the players i got it
ἔρως
ἔρως3mo ago
yes, that is correct
Vandana
VandanaOP3mo ago
Comparision is becomming tricky
ἔρως
ἔρως3mo ago
that's why i said to throw it away it really is a lost cause
Vandana
VandanaOP3mo ago
Meaning?
ἔρως
ἔρως3mo ago
means: start over
Vandana
VandanaOP3mo ago
seriously i am not getting what are you comming to say.
ἔρως
ἔρως3mo ago
im coming at that the approach is not good it's dead on arrival it's just troubles and won't do what you need to stop it and use a different one
Vandana
VandanaOP3mo ago
apart from gettting random number and then comparing what are the other ways🤔
ἔρως
ἔρως3mo ago
not using that get a random key from this, for example
Vandana
VandanaOP3mo ago
const keys = Object.keys(moves); const randomKey = keys[Math.floor(Math.random() * keys.length)]; console.log(randomKey, moves[randomKey]);
ἔρως
ἔρως3mo ago
that's one way to do it, yes that should work
Vandana
VandanaOP3mo ago
Yes exactly now i am getting planning is very very important
ἔρως
ἔρως3mo ago
planning is the essence of a project a project without planning is just a pile of bad code over bad code
Vandana
VandanaOP3mo ago
I was doing this as how everyone normally does
ἔρως
ἔρως3mo ago
nobody sane does that
glutonium
glutonium3mo ago
actually i have seen this exact approach before once as well 😂 but yah I don't think pretty much no one does it like this
ἔρως
ἔρως3mo ago
it doesnt make sense for this project
glutonium
glutonium3mo ago
yep
ἔρως
ἔρως3mo ago
working with the keys is a lot easier and you always guarantee a valid input
Want results from more Discord servers?
Add your server