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