19.6k views
2 votes
1. Write JavaScript function that will take as input a string and check if that string represents a valid 10 digit phone number in the format

2. Write JavaScript program that will replace all numbers in a string with the word "number".

User Pigueiras
by
7.6k points

1 Answer

0 votes

Final answer:

A JavaScript function is provided to validate a 10-digit phone number using a regular expression, and another function is shown to replace all numbers in a string with the word 'number'.

Step-by-step explanation:

JavaScript Function to Validate Phone Number

To verify if a string represents a valid 10-digit phone number, you can use a regular expression within a JavaScript function. The following function takes a string as input and returns true if it's a valid 10-digit number, otherwise, it returns false:

function isValidPhoneNumber(phone) {
var phoneRegex = /^\d{10}$/;
return phoneRegex.test(phone);
}

JavaScript Program to Replace Numbers

The next program aims to replace all numbers in a given string with the word "number". Here's how you can do it with a JavaScript function:

function replaceNumbersWithString(text) {
return text.replace(/\d+/g, 'number');
}

These codes provide a form of data manipulation and validation that are essential in processing numbers and text.

User Ootterskog
by
7.6k points