Compound Conditions

Sometimes, conditions can be more complicated. For example, if we ask a user to enter a word that starts with a letter between A and G, we have to do a little more work. Due to our math backgrounds, many of us would write this condition in the following form: if ("A" < name < "G")
However, the computer cannot translate this statement as the computer can only process ONE comparison operator at a time. So, to make the computer understand, we would have to break this into two statments "A"<name AND name<"G". The use of the word AND is the key here. In programming conditions, AND is represented by the symbol &&. So, the resulting condition would be as follows: if("A"<name && name<"G")
For an AND statement to be true, both parts must be true. There is also an operator for the word OR. That operator is ||. For an OR statement to be true, at least one of the parts must be true. Finally, we can negate conditions using the NOT (!) operator. The NOT operator reverses the truth value of a statement. Finally, a good programming vocab word is inclusive. The way the current questions is asked and coded, the user cannot succeed with a word that starts with A or G, just letters between them. Had we been asked for a letter between A and G, inclusive, that means that A and G are included in the successful results. The only change is that the comparison operators would be <= instead of <.
Nested Conditions
Computer Science books often like to make a big deal about nested conditions (a condition inside another condition). I don't believe this to be a challenge. If we know that within the brackets of an if or else case we can write any valid JavaScript code, and another condition is valid code, then we should be able to do it. Here is an example: if(username=="open"){ if(password=="sesame"){ window.alert("You may enter."); } else{ window.alert("Your password is incorrect."); } } else{ window.alert("Your username is incorrect."); }
That second conditional is valid JavaScript code so it can be placed inside either the if or else section without any issues. Just be careful of opening and closing braces and use indentation in your code to make it readable.

Sometimes, there is a need for a variety of mutually exclusive options (like on a menu). In cases like these, we typically end up nesting several conditionals inside the elses of the statements. It may look something like this:
if(option==1){ window.alert("you chose #1."); } else{ if(option==2){ window.alert("you chose #2."); } else{ if(option==3){ window.alert("you chose #3."); } else{ window.alert("you did not choose a valid option."); } } } This type of nested conditions can often be more easily written in an else...if chain. The above code would look as follows: if(option==1){ window.alert("you chose #1."); } else if(option==2){ window.alert("you chose #2."); } else if(option==3){ window.alert("you chose #3."); } else{ window.alert("you did not choose a valid option."); }