231k views
4 votes
JavaScript

Code Example 4-1
var name = prompt("Enter the name to print on your tee-shirt");
while (name.length > 12) {
name = prompt("Too long. Enter a name with fewer than 12 characters.");
}
alert("The name to be printed is " + name.toUpperCase());
What will happen if the user enters Willy Shakespeare at the first prompt?
A. The user will be prompted to enter a different name.
B. The alert will display The name to be printed is " + name.toUpperCase().
C. The alert will display The name to be printed is WILLY SHAKESPEARE.
D. The alert will display The name to be printed is WILLY SHAKES.

User Enyinnaya
by
5.6k points

1 Answer

6 votes

Answer:

(a) The user will be prompted to enter a different name

Step-by-step explanation:

Given code;

--------------------------------------------------------------------------------------------------------

var name = prompt("Enter the name to print on your tee-shirt");

while (name.length > 12) {

name = prompt("Too long. Enter a name with fewer than 12 characters.");

}

alert("The name to be printed is " + name.toUpperCase());

--------------------------------------------------------------------------------------------------------

=>The first line of the code prompts the user to enter some name to be printed on their tee-shirt, and the input from the user is saved in a variable called name.

=> In the while block, the name variable is checked. If the length of the value of the variable is greater than 12 then a new prompt that says "Too long. Enter a name with fewer than 12 characters" is displayed.

=> In the last line, if the name variable is checked and the length is less than 12, then an alert statement showing the name in uppercase is displayed with some prepended texts.

Now, if the user enters Willy Shakespeare at the first prompt, the while block will be executed since Willy Shakespeare has over 12 characters.

In other words, the prompt in the while block, asking the user to enter a different name will be displayed. i.e

"Too long. Enter a name with fewer than 12 characters"

User Shanice
by
5.4k points