Working with Loops - Exercise

Contact Us or call 1-877-932-8228
Working with Loops - Exercise

Working with Loops

Duration: 20 to 30 minutes.

In this exercise, you will practice working with loops.

  1. Open ConditionalsAndLoops/Exercises/Loops.html for editing. You will see that this file is similar to the solution from the last exercise.
  2. Declare an additional variable called greeting.
  3. Create an array called presidents that contains the last names of four or more past presidents.
  4. Currently, the user only gets two tries to enter a valid gender and lastName. Modify the code so that, in both cases, the user continues to get prompted until he enters valid data.
  5. Change the switch block so that it assigns an appropriate value (e.g, "Hello Ms. Smith") to the greeting variable rather than popping up an alert.
  6. After the switch block, write code that alerts the user by name if he has the same last name as a president. There is no need to alert those people who have non-presidential names.

Challenge

  1. Modify the code so that the first prompt for gender reads "What gender are you: Male or Female?", but all subsequent prompts for gender read "You must enter 'Male' or 'Female'. Try again:".
  2. Modify the code so that the first prompt for last name reads "Enter your last name:", but all subsequent prompts for last name read "Please enter a valid last name:".
  3. If the user presses the Cancel button on a prompt dialog, it returns null. Test this. It very likely results in a JavaScript error. If so, fix the code so that no JavaScript error occurs.
  4. For those people who do not have presidential names, pop up an alert that tells them their names are not presidential.

Solution:

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

<script type="text/javascript">
function greetUser(){
	var gender, lastName, greeting;
	var presidents = ["Washington", "Jefferson", "Lincoln", "Kennedy"];
	
	do {
		gender = prompt("What gender are you: Male or Female?", "") || "";
	} while (gender.toLowerCase() != "male" && gender.toLowerCase() != "female");
	
	do {
		lastName = prompt("Enter your last name:", "") || "";
	} while (lastName.length === 0 || ! isNaN(lastName));
	
	switch (gender.toLowerCase()) {
	case "male" :
		greeting = "Hello Mr. " + lastName + "!";
		break;
	case "female" :
		greeting = "Hello Ms. " + lastName + "!";
		break;
	default :
		greeting = "Hello Stranger!";
	}

	for (var pres in presidents) {
		if (lastName.toLowerCase() == presidents[pres].toLowerCase()) {
			alert(greeting + " You have the same last name as a president!");
		}
	}
}
</script>
---- C O D E   O M I T T E D ----

Challenge Solution:

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

<script type="text/javascript">
function greetUser(){
	var gender, lastName, greeting;
	var presidents = ["Washington", "Jefferson", "Lincoln", "Kennedy"];
	
	var repeat = false;

	do {
		if (!repeat) {
			gender = prompt("What gender are you: Male or Female?", "") || "";
			repeat = true;
		} else {
			gender = prompt("You must enter 'Male' or 'Female'.  Try again:", "") || "";
		}
	} while (typeof gender != "string"
		|| (gender.toLowerCase() != "male"
		&& gender.toLowerCase() != "female"));

	repeat = false;

	do {
		if (!repeat) {
			lastName = prompt("Enter your last name:", "") || "";
			repeat=true;
		} else {
			lastName = prompt("Please enter a valid last name:", "") || "";
		}
	} while (typeof lastName != "string"
		|| (lastName.length === 0
		|| ! isNaN(lastName)));

	switch (gender.toLowerCase()) {
	case "male" :
		greeting = "Hello Mr. " + lastName + "!";
		break;
	case "female" :
		greeting = "Hello Ms. " + lastName + "!";
		break;
	default :
		greeting = "Hello Stranger!";
	}

	var match = false;

	for (var pres in presidents) if (presidents.hasOwnProperty(pres)) {
		if (lastName.toLowerCase() == presidents[pres].toLowerCase()) {
			alert(greeting + " You have the same last name as a president!");
			match = true;
		}
	}

	if (!match) {
		alert(greeting + " Your last name is not presidential.");
	}
}
</script>
---- C O D E   O M I T T E D ----
Next