While writing the JavaScript code for this project, everything is working except for Step 35 and Step 36.
Step 35: When the #user-input element contains a valid US number and the #check-btn element is clicked, the #results-div element should contain the text "Valid US number: " followed by the number.
Step 36: When the #user-input element contains an invalid US number and the #check-btn element is clicked, the #results-div element should contain the text "Invalid US number: " followed by the number.
However, I believe I have already included this in my code. Can you help me understand why it is not working?
JS code:
const checkBtn = document.getElementById('check-btn');
const clearBtn = document.getElementById('clear-btn');
const resultsDiv = document.getElementById('results-div');
checkBtn.addEventListener('click', () => {
const userInput = document.getElementById('user-input').value;
const regex = /^(1\s?)?(\(\d{3}\)|\d{3})[\s\-]?\d{3}[\s\-]?\d{4}$/;
if (!userInput) {
alert('Please provide a phone number');
resultsDiv.textContent = `Please provide a phone number`
} else if (regex.test(userInput)) {
resultsDiv.textContent = `Valid US number: ${userInput}`;
} else {
resultsDiv.textContent = `Invalid US number: ${userInput}`;
}
});
clearBtn.addEventListener('click', () => {
resultsDiv.textContent = '';
document.getElementById('user-input').value = '';
});
When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
hi @joelmmasikini please create your own topic to ask for help, and do not ask to copy other people’s code, that’s against the Academic Honesty Pledge
If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Get Help > Ask for Help button located on the challenge.
The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.