Helping on a certain part I don't understand about JS {method if else ect}

Hi people I am currently learning JS on the code academy classes but....I am stuck on a part where I just don't understand a certain exercise where I have to make a valor between a certain number for show something....I think the example will show what I have to do lol

var choixUtilisateur = prompt("Question");var choixOrdi = Math.random(1)console.log(choixOrdi)if (choixOrdi <= 0.33){ choixOrdie = "pierre";} else if(choixOrdi >= 0.34 && choixOrdi <= 0.66){ choixOrdie = "feuille";} else if(choixOrdi > 0.67 && choixOrdi <= 1) { choixOrdi = "ciseau";}for some reason this not works ...here the lesson I am on : 

https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/0/4?curriculum_id=506324b3a7dffd00020bf661

I hope you guys could help me...I block on this part 🙁
Uhm, you are leaving gaps. There are plenty of numbers between 0.33 and 0.34 such as 0.3346.

Let one of the conditions be <= 0.33 and the other strictly greater than 0.33. I.e. use  > 0.33.

Does this make sense?
sadly they throw me a error like that ...I don't understand I inputed a valor for it 🙁

"Oops, try again thank you. Have you given a value of 'scissors' to choixOrdi between 0.67 and 1?"
Interesting. The gaps is a part of the exercise.

I copy-pasted your code and replaced the French words with the English equivalences. It works fine for me.

Code:
var choixUtilisateur = prompt("Question");var choixOrdi = Math.random(1)console.log(choixOrdi)if (choixOrdi <= 0.33){    choixOrdie = "rock";} else if(choixOrdi >= 0.34 && choixOrdi <= 0.66){    choixOrdie = "paper";} else if(choixOrdi > 0.67 && choixOrdi <= 1) {    choixOrdi = "scissors";}
Interesting. The gaps is a part of the exercise.

I copy-pasted your code and replaced the French words with the English equivalences. It works fine for me.

var choixUtilisateur = prompt("Question");var choixOrdi = Math.random(1)console.log(choixOrdi)if (choixOrdi <= 0.33){    choixOrdie = "rock";} else if(choixOrdi >= 0.34 && choixOrdi <= 0.66){    choixOrdie = "paper";} else if(choixOrdi > 0.67 && choixOrdi <= 1) {    choixOrdi = "scissors";}
damn this was a Misspelling words Errors and yeah your gap seem better than mine thanks you : D but I find...math really more harder in JS than ruby :/
No, I mean. When dealing with floating points normally you'd not allow gaps.

For example 0.333 would not choose anything. According to the exercise specification that is not wrong. Normally I'd do it this way instead:

var choixUtilisateur = prompt("Question");var choixOrdi = Math.random(1)console.log(choixOrdi)if (choixOrdi <= 0.33){    choixOrdie = "rock";} else if(choixOrdi > 0.33 && choixOrdi <= 0.66){    choixOrdie = "paper";} else if(choixOrdi > 0.66 && choixOrdi <= 1) {    choixOrdi = "scissors";}
*hugs*
No, I mean. When dealing with floating points normally you'd not allow gaps.

For example 0.333 would not choose anything. According to the exercise specification that is not wrong. Normally I'd do it this way instead:

var choixUtilisateur = prompt("Question");var choixOrdi = Math.random(1)console.log(choixOrdi)if (choixOrdi <= 0.33){    choixOrdie = "rock";} else if(choixOrdi > 0.33 && choixOrdi <= 0.66){    choixOrdie = "paper";} else if(choixOrdi > 0.66 && choixOrdi <= 1) {    choixOrdi = "scissors";}*hugs*
I see you have good point here ! I never knew that about float number but if this was not float number but integer it's would be possible to give some gaps?
The tricky thing about floating point is that you should not do x == 0.65 tests, but rather if (0.65 - threshold <= x && x <= 0.65 + threshold). The same holds true for Ruby btw.

As for integers. If you had a random integer between 0 and 100, then doing <= 33 and >= 34 would be fine.

*hugs*
The tricky thing about floating point is that you should not do x == 0.65 tests, but rather if (0.65 - threshold <= x && x <= 0.65 + threshold). The same holds true for Ruby btw.

As for integers. If you had a random integer between 0 and 100, then doing <= 33 and >= 34 would be fine.

*hugs*
could you mind to explain me what's threshold stand for 😮 ?
It is the accepted error bounds. We could for example set it to 0.00001
It is the accepted error bounds. We could for example set it to 0.00001
okai thanks : D 
Yeah, floats are a bit not that accurate... Normally I try to use a buffer of around .0001when I worked with them on ruby for my projectile collision or so, depends on testing ofc... Back on VB I try to use around 0.1% to 1% variance..
var choixUtilisateur = prompt("Question");var choixOrdi = floor(100 * Math.random(1));console.log(choixOrdi)if (choixOrdi <= 33){ choixOrdie = "rock";} else if(choixOrdi > 33 && choixOrdi <= 66){ choixOrdie = "paper";} else if(choixOrdi > 66 && choixOrdi <= 100) { choixOrdi = "scissors";}perhaps? Not sure if that's the correct syntax for floor. And it probably wouldn't be == 100 if rand(x) produces a number >= 0 and < x.Does the fact that the last two lines here are missing the ; mean anything?

Code:
var choixUtilisateur = prompt("Question");var choixOrdi = Math.random(1)console.log(choixOrdi)
I recommend reading this, as there's a lot of smaller details surrounding semicolon and how and why it's used: http://inimino.org/~inimino/blog/javascript_semicolons
I have to notices something:

var choixUtilisateur = prompt("Question");var choixOrdi = floor(100 * Math.random(1));console.log(choixOrdi) // <- automatic semicolon insertion (wich is a good-bad feature) try to avoid itHere is an article to why you should avoid them.

But I tested it on my chrome console and it worked, so not a real problem...

And yes, you should check for numbers between 0.33 and 0.34 as:

var choixUtilisateur = prompt("Question");var choixOrdi = Math.random(1);console.log(choixOrdi);if (choixOrdi <= 0.33) {    choixOrdie = "pierre";} else if ( choixOrdi < 0.67 ) {    choixOrdie = "feuille";} else {    choixOrdi = "ciseau";}Yo don't really need to validate if it's bigger than 0.33, it will always be bigger than 0.33 because it is already in the else clause.

The same happens in the last clause

And the same applies to checking if it's bigger than 1. Math.random will never give a bigger number than 1.

And now... your code is magically shorter 😀
This doesn't seem like a very good exercise, given that anything between those bounds have undefined behavior.


I would've opted to use strictly integers.

damn this was a Misspelling words Errors and yeah your gap seem better than mine thanks you : D but I find...math really more harder in JS than ruby :/
Yes, these exercises are very specific in their outputs. You have to follow them exactly as they say (including language).


"rock" in this case does not mean anything and is just a string of characters. They could have just made minion sounds.
actually I just follow how they explain me if I don't follow them they will throw me a error :/ but it's generally just for learn the basis after I will just get rid of the old learning by learning basis by myself or checking how rm MV structures will works...this classes in codeacademy just help me to patch my lack of knowledge on some part and I for help me to understand some part by practice ...it's not really by reading hyper complex english documentation I can really understand lol...I have to have context and practice for understand them 

{people have to remember I am a non native english so big walls of text could just confuse me more then I though­}

but like I said I still have the ruby knowledge in the head so after JS is really easy just I have to get used of the new syntax haha

but thanks guys for helping me out and shaz I am not sure of what you mean 🙁

Original thread: https://forums.rpgmakerweb.com/threads/helping-on-a-certain-part-i-dont-understand-about-js-method-if-else-ect.43625/