Conditional Processing - Exercise

Contact Us or call 1-877-932-8228
Conditional Processing - Exercise

Conditional Processing

Duration: 20 to 30 minutes.

In this exercise, you will practice using conditional processing.

  1. Open ConditionalsAndLoops/Exercises/Conditionals.html for editing.
  2. Notice that there is an onload event handler that calls the greetUser() function. Create this function in the script block.
  3. The function should do the following:
    1. Prompt the user for his/her gender and last name and store the results in variables.
    2. If the user enters a gender other than "Male" or "Female", prompt him/her to try again.
    3. If the user leaves the last name blank, prompt him/her to try again.
    4. If the user enters a number for the last name, tell him/her that a last name can't be a number and prompt him/her to try again.
    5. After collecting the gender and last name:
      • If the gender is valid, pop up an alert that greets the user appropriately (e.g, "Hello Ms. Smith!")
      • If the gender is not valid, pop up an alert that reads something like "XYZ is not a gender!"
  4. Test your solution in a browser.

Challenge

  1. Allow the user to enter his/her gender in any case.
  2. If the user enters a last name that does not start with a capital letter, prompt him/her to try again.

Solution:

ConditionalsAndLoops/Solutions/Conditionals.html
---- C O D E   O M I T T E D ----

<script type="text/javascript">
function greetUser(){
	var gender, lastName;
	
	gender = prompt("Are you Male or Female?", "") || "";
	if (gender != "Male" && gender != "Female") {
		gender = prompt("Try again: Male or Female?", "") || "";
	}
	
	lastName = prompt("What's your last name?", "") || "";
	if (lastName.length === 0) {
		lastName = prompt("You must have a last name.  Please re-enter:", "") || "";
	} else if (! isNaN(lastName)) {
		lastName = prompt("A last name can't be a number.  Please re-enter:", "") || "";
	}
	
	switch (gender) {
	case "Male" :
		alert("Hello Mr. " + lastName + "!");
		break;
	case "Female" :
		alert("Hello Ms. " + lastName + "!");
		break;
	default :
		alert(gender + " is not a gender!");
	}
}
</script>

---- C O D E   O M I T T E D ----

Challenge Solution:

ConditionalsAndLoops/Solutions/Conditionals-challenge.html
---- C O D E   O M I T T E D ----

<script type="text/javascript">
function greetUser(){
	var gender, lastName;
	
	gender = prompt("Are you Male or Female?", "") || "";
	if (gender.toLowerCase() != "male" && gender.toLowerCase() != "female") {
		gender = prompt("Try again: Male or Female?", "") || "";
	}
	
	lastName = prompt("What's your last name?", "");
	if (lastName.length === 0) {
		lastName = prompt("You must have a last name.  Please re-enter:", "");
	} else if (! isNaN(lastName)) {
		lastName = prompt("A last name can't be a number.  Please re-enter:", "");
	} else if (lastName.substr(0, 1) == lastName.substr(0, 1).toLowerCase()) {
		lastName = prompt("The first letter must be capitalized.  Re-enter:", "") || "";
	}
	
	switch (gender.toLowerCase()) {
	case "male" :
		alert("Hello Mr. " + lastName + "!");
		break;
	case "female" :
		alert("Hello Ms. " + lastName + "!");
		break;
	default :
		alert(gender + " is not a gender!");
	}
}
</script>
---- C O D E   O M I T T E D ----
Next