Navigation




Exercise: The Name Game

Try this program:

Simple name program

1. Add a middle name. The program should ask the user for it, then place it in the appropriate position.

2. Go back to the original program. Try adding something like this:

firstName = prompt("What is your first name?", "");
if ( firstName == "" ) {
    firstName = "Jimbo";
	alert("No first name? I will call you " + firstName);
}

What does it do?

Do the same thing for the last name.

3. Go back to the original program. Change the program so that it outputs "Last name, first name", e.g.,

Jones, Jimbo

4. Go back to the original program. Change the program so that it outputs in uppercase:

JONES, JIMBO

Hint: try toUpperCase(), like fullName.toUpperCase()

(Solution) <- DO IT YOURSELF FIRST!!

5. Go back to the original program. Change it so that it outputs the user's initials as well, e.g., JJ for Jimbo Jones.

Hint: Check out charAt(), like firstName.charAt(0).

6. Change the program in 5 so that the initials are always output in uppercase, even if the user entered lowercase names.

(Solution) <- DO IT YOURSELF FIRST!!

7. Change the program in 6 so that the first characters of the names are always output in uppercase, even if the user entered them in lowercase.

(Solution) <- DO IT YOURSELF FIRST!!

8. Go get some coffee and chill.

< Back to Introducing JS