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

+2
Answers (1)
  1. 29 April, 11:38
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers