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

RE-SUBMIT THIS AS PURE JAVASCRIPT (no java libraries!) . import java.util.Scanne

ID: 3770750 • Letter: R

Question

RE-SUBMIT THIS AS PURE JAVASCRIPT (no java libraries!)

. import java.util.Scanner; public class TelephoneNumberTranslator { public static boolean checkFormat(String number) { if (number.length() != 12) { return false; } for (int i = 0; i < number.length(); ++i) { if (i == 3 || i == 7) { if (number.charAt(i) != '-') { return false; } } else if (!(number.charAt(i) >= '0' && number.charAt(i) <= '9') && !(number.charAt(i) >= 'a' && number.charAt(i) <= 'z') && !(number.charAt(i) >= 'A' && number.charAt(i) <= 'Z')){ return false; } } return true; } public static char getIntegerValue(char ch) { if (ch >= 'a' && ch <= 'z') { ch -= 32; } if (ch == 'A' || ch == 'B' || ch == 'C') { return '2'; } else if (ch == 'D' || ch == 'E' || ch == 'F') { return '3'; } else if (ch == 'G' || ch == 'H' || ch == 'I') { return '4'; } else if (ch == 'J' || ch == 'K' || ch == 'L') { return '5'; } else if (ch == 'M' || ch == 'N' || ch == 'O') { return '6'; } else if (ch == 'P' || ch == 'Q' || ch == 'R' || ch == 'S') { return '7'; } else if (ch == 'T' || ch == 'U' || ch == 'V') { return '8'; } else if (ch == 'W' || ch == 'X' || ch == 'Y' || ch == 'Z') { return '9'; } else { return ch; } } public static String getNumber(String number) { String result = ""; for (int i = 0; i < number.length(); ++i) { result += getIntegerValue(number.charAt(i)); } return result; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String number = null; while (true) { System.out .print("enter a 10-character telephone number in the format XXX-XXX-XXXX: "); number = in.next(); if(checkFormat(number) == true){ break; } else{ System.out.println("Invalid Format. Try Again!!"); } } System.out.println("Numerical Representation of " + number + " is " + getNumber(number)); } } Original Problem: Design a JavaScript program: 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, 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.

(NO JAVA LIBRARIES, NO "import java.util.Scanner;") please

Explanation / Answer

filename.html

<html>
<body>
<p> Enter a 10-character telephone number </P>
<form name="myform">
       <input type="text" name="phone" value="XXX-XXX-XXXX"/>
       <input class="sty" type="button" name="submit" value="Get Number"/>
</form>
<p id="number"></p>
<script>
   function checkFormat(number)
   {
       if (number.length != 12)
       {
           return false;
       }
       for (var i = 0; i < number.length; i++)
       {
           if (i == 3 || i == 7)
           {
               if (number.charAt(i) != '-')
               {
                   return false;
               }
           }
           else if (!(number.charAt(i) >= '0' && number.charAt(i) <= '9') && !(number.charAt(i) >= 'a' && number.charAt(i) <= 'z') && !(number.charAt(i) >= 'A' && number.charAt(i) <= 'Z'))
           {
               return false;
           }
       }
       return true;
   }
   function getIntegerValue(ch)
   {
       ch = ch.toUpperCase();
       if (ch == 'A' || ch == 'B' || ch == 'C')
       {
           return '2';
       }
       else if (ch == 'D' || ch == 'E' || ch == 'F')
       {
           return '3';
       }
       else if (ch == 'G' || ch == 'H' || ch == 'I')
       {
           return '4';
       }
       else if (ch == 'J' || ch == 'K' || ch == 'L')
       {
           return '5';
       }
       else if (ch == 'M' || ch == 'N' || ch == 'O')
       {
           return '6';
       }
       else if (ch == 'P' || ch == 'Q' || ch == 'R' || ch == 'S')
       {
           return '7';
       }
       else if (ch == 'T' || ch == 'U' || ch == 'V')
       {
           return '8';
       }
       else if (ch == 'W' || ch == 'X' || ch == 'Y' || ch == 'Z')
       {
           return '9';
       }
       else
       {
           return ch;
       }
   }
   function getNumber(form)
   {
       var result = "";
       var number = form.phone.value;
       if(checkFormat(number))
       {
           for (var i = 0; i < number.length; ++i)
           {
               result += getIntegerValue(number.charAt(i));
           }
           document.getElementById("number").innerHTML = "Numerical Representation of " + number + " is " + result;
       }
       else
       {
           document.getElementById("number").innerHTML = "Invalid Format. Try Again!!";
       }
   }
</script>
</body>
</html>