Jump to content

I do not understand this


tapasa

Recommended Posts

var sexo; sexo=prompt("Introduce el sexo del alumno");while (sexo !== "h" && sexo !== "H" && sexo !== "M" && sexo !== "m"){	alert("sexo erróneo: " + sexo + ". Teclea de nuevo");	sexo=prompt("Introduce el sexo del alumno (Hombre(H o h), Mujer(M o m))");}alert("El sexo es: " + sexo + " es correcto");

 

//This code above is right, but i cannot understand why, the sexo var must be "h" or "H" or "m" or "M" to break the while. Why if i put || instead of && it doesnt work and stay in the while?

 

- Many thanks for you attention ^^

Link to comment
Share on other sites

To answer your first question, the reason is to make sure they've given a proper answer and not letting them continue until they do. The only answers allowed are "h", "H", "m" and "M", if other answers are given they're asked the question again.

 

The answer to your second question is logic.

 

You only want to ask the question again if all four of the conditions are met. The conditions being that the answer is not "h", not "H", not "m" and not "M". If you used an || operator then the condition is always met, since at least one of the four conditions is always true. If it's "H" then not "M" is true, if it's "M" then not "H" is true.

Link to comment
Share on other sites

To answer your first question, the reason is to make sure they've given a proper answer and not letting them continue until they do. The only answers allowed are "h", "H", "m" and "M", if other answers are given they're asked the question again.

 

The answer to your second question is logic.

 

You only want to ask the question again if all four of the conditions are met. The conditions being that the answer is not "h", not "H", not "m" and not "M". If you used an || operator then the condition is always met, since at least one of the four conditions is always true. If it's "H" then not "M" is true, if it's "M" then not "H" is true.

Yeah, I understand the first part, but why the && and not ||. Sorry I'm spanish and I dont understand things like this in english haha. I understood the first part, but the condition is var sexo must be one of these, i first thought in || but my teacher told me that the || is not correct and the while never ends, but in && it works, but i think the condition catch all and must be sexo = hHmM, all at same time in &&, and in || the sexo var with one of the condition breaks, but not...this is that i cant understand

Edited by tapasa
Link to comment
Share on other sites

while (sexo !== "h" && sexo !== "H" && sexo !== "M" && sexo !== "m"){  // not done yet}while (!(sexo == "h" || sexo == "H" || sexo == "M" || sexo == "m")){  // not done yet}

...but to me that seems confusing so I would prefer to use a variable...

var done = false;while (!done){  // not done yet  if (sexo == "h" || sexo == "H" || sexo == "M" || sexo == "m"){     done = true;  }  }
Link to comment
Share on other sites

while (sexo !== "h" && sexo !== "H" && sexo !== "M" && sexo !== "m"){  // not done yet}while (sexo !== "h" || sexo !== "H" || sexo !== "M" || sexo !== "m"){  // not done yet}

i think in theory the second code must work, was the first thingh i thought, but not, and i cant understand why the first yes and the other not :/

Link to comment
Share on other sites

Your understanding of the logical operators is incorrect. This is an application of De Morgan's laws: http://en.wikipedia.org/wiki/De_Morgan%27s_laws

 

If you were not negating the inputs then the result would need the OR operator.

 

Let's assume these conditions:

P: sexo == "h"

Q: sexo == "H"

R: sexo == "M"

S: sexo == "m"

These would be the negated conditions:

¬P: sexo !== "h"

¬Q: sexo !== "H"

¬R: sexo !== "M"

¬S: sexo !== "m"

 

This is the correct answer that the user should provide:

P || Q || R || S

 

The while loop is checking that the user gave the incorrect answer:

¬(P || Q || R || S)

 

which, due to De Morgan's law, is equivalent to:

¬P && ¬Q && ¬R && ¬S

Link to comment
Share on other sites

The first code uses the && operator. The && operator evaluates all conditions from left to right and returns 'true' if all the conditions are satisfied. If one is satisfied, it continues on to the next until all conditions are tested. All must be satisfied for the instruction to be executed. The || operator tests if one of the conditions is true. If the first is false, the second is evaluated. But if the first is true, the other conditions bound by the || will be ignored and the instruction executed.So, considering your code, the first loop tests the variable 'sexo" for all predefined values. It must satisfy all conditions, returning 'true' for the code to run. If sexo === 'h', for example, it returns false and the loop ends without executing the code. The second code tests for options. If sexo === 'h', being an OR operator, it executes the instruction if any of the conditions is true. That means one of the conditions must definitely be true in this case. And so, the loop won't end.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...