Ask Question
25 February, 05:34

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.

+1
Answers (1)
  1. 25 February, 06:13
    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?
Get 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 & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers