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
VandanaOP4mo 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
glutonium4mo 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
VandanaOP4mo ago
So this may result in errors ?
ἔρως
ἔρως4mo 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
VandanaOP4mo 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).
ἔρως
ἔρως4mo ago
you're right, but 0.3333... isn't 0.3333.... 1/3 is actually 0.3333333730697632
glutonium
glutonium4mo ago
knew that was coming
ἔρως
ἔρως4mo 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
ἔρως
ἔρως4mo 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; }
ἔρως
ἔρως4mo ago
it's overkill, but unbiased
Vandana
VandanaOP4mo ago
This is so frustrating then why in the console it gives 1/3 as 0.3333333333
ἔρως
ἔρως4mo ago
because javascript is tricking you it's hidding the error part from you
Vandana
VandanaOP4mo ago
how can i scan when its hiding ?when its not hiding
ἔρως
ἔρως4mo ago
no idea ¯\_(ツ)_/¯ always assume all floats are wrong
Vandana
VandanaOP4mo ago
then this gives error too while cabing
ἔρως
ἔρως4mo ago
my answer depends on the rest of your code can you show it?
Vandana
VandanaOP4mo 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>
ἔρως
ἔρως4mo ago
so, you don't even have a way for the player to pick an option?
Vandana
VandanaOP4mo ago
I was using 1/3 actually I am still working on it
ἔρως
ἔρως4mo ago
you've started """"wrong"""" you don't have anywhere that has the possible values
Vandana
VandanaOP4mo ago
Among three buttons he will click one which he wants either rock or paper or scissor which values?
ἔρως
ἔρως4mo 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
VandanaOP4mo ago
yes this is the further modification
ἔρως
ἔρως4mo ago
it should be the first step you have to know the valid inputs before you write anything planning is extremely important
Vandana
VandanaOP4mo ago
ohh <button onclick="playGame('Rock')">Rock</button> <button>Paper</button> <button>Scissors</button> i have planned na
ἔρως
ἔρως4mo 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
VandanaOP4mo ago
YES As the first step right?
ἔρως
ἔρως4mo ago
yes, as part of the ui
Vandana
VandanaOP4mo ago
instead of onClick we can use event listeners.
ἔρως
ἔρως4mo ago
instead of 3 onclicks, you can have 1 onsubmit on the form
Vandana
VandanaOP4mo ago
This 1 onSubmit gives any value right either rock,paper,or scissor
ἔρως
ἔρως4mo 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
VandanaOP4mo ago
yes getting my main doubt is about the float condition comparision What can i do to avoid the float issues
ἔρως
ἔρως4mo ago
nothing, throw away the code
Vandana
VandanaOP4mo ago
this is a way to pick option for the players i got it
ἔρως
ἔρως4mo ago
yes, that is correct
Vandana
VandanaOP4mo ago
Comparision is becomming tricky
ἔρως
ἔρως4mo ago
that's why i said to throw it away it really is a lost cause
Vandana
VandanaOP4mo ago
Meaning?
ἔρως
ἔρως4mo ago
means: start over
Vandana
VandanaOP4mo ago
seriously i am not getting what are you comming to say.
ἔρως
ἔρως4mo 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
VandanaOP4mo ago
apart from gettting random number and then comparing what are the other ways🤔
ἔρως
ἔρως4mo ago
not using that get a random key from this, for example
Vandana
VandanaOP4mo ago
const keys = Object.keys(moves); const randomKey = keys[Math.floor(Math.random() * keys.length)]; console.log(randomKey, moves[randomKey]);
ἔρως
ἔρως4mo ago
that's one way to do it, yes that should work
Vandana
VandanaOP4mo ago
Yes exactly now i am getting planning is very very important
ἔρως
ἔρως4mo ago
planning is the essence of a project a project without planning is just a pile of bad code over bad code
Vandana
VandanaOP4mo ago
I was doing this as how everyone normally does
ἔρως
ἔρως4mo ago
nobody sane does that
glutonium
glutonium4mo 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
ἔρως
ἔρως4mo ago
it doesnt make sense for this project
glutonium
glutonium4mo ago
yep
ἔρως
ἔρως4mo 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