Ask Question
14 August, 09:11

Write a program that inserts parentheses, a space, and a dash into a string of 10 user-entered numbers to format it as a phone number. For example, 5153458912 becomes (515) 345-8912. If the user does not enter exactly 10 digits, display an error message. Continue to accept user input until the user enters 999.

+5
Answers (1)
  1. 14 August, 09:33
    0
    const readline = require ('readline-sync');

    let reg = / ^ (/d{3}) (/d{3}) (/d{4}) $/;

    do {

    let number = readline. question ("Enter a phone number: ");

    if (number = = = '999') {

    process. exit ();

    }

    var r = number. match (reg);

    if (! r) console. log ("That is not right.");

    else {

    console. log (' (${r[1]}) ${r[2]}-${r[3]}');

    }

    } while (true);

    Explanation:

    This is a jа vascript solution using regular expressions.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a program that inserts parentheses, a space, and a dash into a string of 10 user-entered numbers to format it as a phone number. For ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers