Answer:
var username; // username entered by user
var charAny; // text character identified in username
var anyNum = false; // digit variable used to detect whether the username has one or not
var index; // index loop variable
var BR = "<br />"; //break
var ES = ""; //space
// Display program requirements for the username requested
document.write("We'll begin helping you select a username" + BR);
document.write("Your username must have at least 8 characters," + BR);
document.write(" start with a letter, and contain at least 1 numeric character." + BR);
username = prompt("Please enter your username: ", ES);
// Check for length of username
while (username.length < 8) {
document.write("Your username must be at least 8 characters long." + BR);
username = prompt("Please enter your username: ", ES);
}
// Check that first character is a letter
// Substring function has three arguments: string, starting position, and ending position
charAny = username.substr(0, 1);
while (charAny !== isLetter()) {
document.write("The first character of your username must be a letter." + BR);
username = prompt("Please enter your username: ", ES);
}
// Check that there's at least one digit in the username
while (anyNum !== false) {
// Check each character, set anyNum to true if a digit
for (index = 1; index < username.substr(index, index); index++) {
anyNum = username.substr(index, index);
if (isNumeric(charAny)) {
anyNum = true;
}
}
// If anyNum is false there were no numerics
if (anyNum !== true) {
document.write("Your username must include at least 1 digit." + BR);
username = prompt("Please enter your username: ", ES);
}
}