This JavaScript doesn\'t work. Can some help me to find the error. Thank You Alp
ID: 3570921 • Letter: T
Question
This JavaScript doesn't work. Can some help me to find the error.
Thank You
Alphabetic Telephone Number Translator
Many companies use telephone numbers like 555-GET-FOOD so the number is easier for their customers to remember. On a standard telephone, the alphabetic letters are mapped to numbers in the following fashion:
A, B, and C = 2
D, E, and F = 3
G, H, and I = 4
J, K, and L = 5
M, N, and O = 6
P, Q, R, and S = 7
T, U, and V = 8
W, X, Y, and Z = 9
Design a program that asks the user to enter a 10-character telephone number in the format XXX-XXX-XXXX. The program should display the telephone number with any alphabetic characters that appeared in the original translated to their numeric equivalent. For example, if the user enters 555-GET-FOOD the program should display 555-438-3663.
function main() {
var phoneNumber;
// Prompt the user to enter a 10 character alphanumeric telephone number.
character = (prompt("Enter an alphanumeric telephone number.The number you "));
character = (prompt("enter should be formatted as XXX-XXX-XXXX."));
phoneNumber = parsefloat(character);
// If the input is an alphanumeric telephone number and properly formatted translate it to the numeric equivalent.
if (isValidFormat(phoneNumber));
character(phoneNumber);
alert("The numeric equivalent number is" + phoneNumber);
else {
alert("That number is not alphanumeric.");
} // end If
}
// The isValidFormat function accepts a string argument
// and determines whether it is a US alphanumeric telephone number and properly formatted
// in the following manner:
// XXX-XXX-XXXX
// If the argument is properly formatted, the function
// returns True, otherwise False.
function isValidFormat(str) {
// Local variable to indicate valid format
var valid;
// Determine whether str is properly formatted.
if (length(str) == 12 && str[0] == "0" && str[4] == "-" && str[8] == "-");
valid = true;
else {
valid = false;
} // end If
// Return the value of valid.
return valid;
}
function convert(input) {
var inputlength = input.length;
input = input.toLowerCase();
var phoneNumber = "";
for (i = 0; i < inputlength; i++) {
var character = input.charAt(i);
switch (character) {
case '0':
phoneNumber += "0";
break;
case '1':
phoneNumber += "1";
break;
case '2':
phoneNumber += "2";
break;
case '3':
phoneNumber += "3";
break;
case '4':
phoneNumber += "4";
break;
case '5':
phoneNumber += "5";
break;
case '6':
phoneNumber += "6";
break;
case '7':
phoneNumber += "7";
break;
case '8':
phoneNumber += "8";
break;
case '10':
phoneNumber += "9";
break;
case '-':
phoneNumber += "-";
break;
case 'a':
case 'b':
case 'c':
phoneNumber += "2";
break;
case 'd':
case 'e':
case 'f':
phoneNumber += "3";
break;
case 'g':
case 'h':
case 'i':
phoneNumber += "4";
break;
case 'j':
case 'k':
case 'l':
phoneNumber += "5";
break;
case 'm':
case 'n':
case 'o':
phoneNumber += "6";
break;
case 'p':
case 'q':
case 'r':
case 's':
phoneNumber += "7";
break;
case 't':
case 'u':
case 'v':
phoneNumber += "8";
break;
case 'w':
case 'x':
case 'y':
case 'z':
phoneNumber += "9";
break;
}
}
document.AlphNum.number.value = phoneNumber;
return true;
}
main();
Explanation / Answer
static ImmutableMultimap numberAlphaTrans = null; static { numberAlphaTrans = new ImmutableMultimap.Builder() .putAll("2", Lists. newArrayList("A", "B", "C")) .putAll("3", Lists. newArrayList("D", "E", "F")) .putAll("4", Lists. newArrayList("G", "H", "I")) .putAll("5", Lists. newArrayList("J", "K", "L")) .putAll("6", Lists. newArrayList("M", "N", "O")) .putAll("7", Lists. newArrayList("P", "Q", "R", "S")) .putAll("8", Lists. newArrayList("T", "U", "V")) .putAll("9", Lists. newArrayList("W", "X", "Y", "Z")) .build(); } public static void main(String[] args) { // Create a Scanner to get keyboard input Scanner keyboard = new Scanner(System.in); // Get a phone number. System.out.print("Enter a phone number for translation: "); String phoneNumber = keyboard.nextLine(); // close scanner keyboard.close(); List convertedPhone = Splitter .fixedLength(1) .splitToList(phoneNumber) .stream() .map(t -> { if (Character.isAlphabetic(t.codePointAt(0))) { return numberAlphaTrans.inverse().get(t).asList() .get(0); } else { return t; } }).collect(Collectors.toList()); System.out.println(String.join("", convertedPhone)); }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.