Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Module 9 JavaScript Program Instructions Please submit a JavaScript program that

ID: 3597098 • Letter: M

Question

Module 9 JavaScript Program

Instructions

Please submit a JavaScript program that meets the requirements of the Alphabetic Telephone Number Translator (Exercise #6) on page 496-7.

This program will use a little bit of everything, arrays/strings, functions.

6. 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.

Explanation / Answer

var alpha_dict = {}
alpha_dict['A'] = 2
alpha_dict['B'] = 2
alpha_dict['C'] = 2

alpha_dict['D'] = 3
alpha_dict['E'] = 3
alpha_dict['F'] = 3

alpha_dict['G'] = 4
alpha_dict['H'] = 4
alpha_dict['I'] = 4

alpha_dict['J'] = 5
alpha_dict['K'] = 5
alpha_dict['L'] = 5

alpha_dict['M'] = 6
alpha_dict['N'] = 6
alpha_dict['O'] = 6

alpha_dict['P'] = 7
alpha_dict['Q'] = 7
alpha_dict['R'] = 7

alpha_dict['T'] = 8
alpha_dict['U'] = 8
alpha_dict['V'] = 8

alpha_dict['W'] = 9
alpha_dict['X'] = 9
alpha_dict['Y'] = 9
alpha_dict['Z'] = 9

function numbericFromAlpha(alpha) {
if (alpha in alpha_dict)
return alpha_dict[alpha];
return alpha;
}

function isNumberValid(number){
if (number.length != 12 || number[3] != "-" || number[7] != "-")
return false;
var indx = [0,1,2,4,5,6,8,9,10,11];
for(var i = 0; i < indx.length; i++){
if (isNaN(number[i]) && (number[i] != number[i].toUpperCase()))
return false;
}
return true;
}

function getNumbericPhoneNumber(number) {
if (!isNumberValid(number)) {
console.log("Enter a valid number");
return -1;
}
  
var convertedNumber = ""
for (var i = 0; i < number.length; i++){
convertedNumber += numbericFromAlpha(number[i]);
}
return convertedNumber;
}

Sample run

console.log(getNumbericPhoneNumber("555-GET-FOOD"))

555-438-3663

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote