Alphabetic Telephone Number Translator Many companies use telephone numbers like
ID: 3812834 • Letter: A
Question
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:
Design a java script 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
<!DOCTYPE html>
<html>
<body>
<h3>Alphabetic Telephone Number Translator</h3>
<input type="text" id="myNumber" value="">
<button>Go</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myNumber").value; //store input string in x
var number_array = new Array();
number_array = x.split('');
for(var i=0; i< x.length; i++){ //access each element and check its value using switch cases
switch(number_array[i]){
case 'A':
case 'B':
case 'C':
number_array[i] = "2";
break;
case 'D':
case 'E':
case 'F':
number_array[i] = "3";
break;
case 'G':
case 'H':
case 'I':
number_array[i] = "4";
break;
case 'J':
case 'K':
case 'L':
number_array[i] = "5";
break;
case 'M':
case 'N':
case 'O':
number_array[i] = "6";
break;
case 'P':
case 'Q':
case 'R':
case 'S':
number_array[i] = "7";
break;
case 'T':
case 'U':
case 'V':
number_array[i] = "8";
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
number_array[i] = "9";
break;
}
}
x = number_array.join('') ; //bind it back to the string //we use .join() to remove comma separators
document.getElementById("demo").innerHTML = x; //display string
}
</script>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.