Tell us what’s happening:
Hi, every test is passing except test 36 and I don’t know why. I’ve looked up the solution on the forums and apparently its a bug, and you have to declare your input variable in the global scope, which I have done
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<title>Telephone Number Validator</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<input id="user-input">
<button id="check-btn">Check</button>
<button id="clear-btn">Clear</button>
<div id="results-div"></div>
<script src="script.js"></script>
</body>
</html>
/* file: styles.css */
/* file: script.js */
const userInput = document.getElementById("user-input");
const checkBtn = document.getElementById("check-btn");
const resultsDiv = document.getElementById("results-div");
const clearBtn = document.getElementById("clear-btn");
checkBtn.addEventListener("click", () => {
const userInputValue = userInput.value;
if (userInputValue === "") {
alert("Please provide a phone number")
}
else {
checkNum(userInputValue)
}
})
function checkNum(input) {
const regex1 = /(?<!-)(?<!\d)(1)\s(\d{3})-(\d{3})-(\d{3})/g;
const regex2 = /(?<!-)(?<!\d)(1)\s([(]\d{3}[)])\s(\d{3})-(\d{4})/g;
const regex3 = /(?<!-)(?<!\d)(1)([(]\d{3}[)])(\d{3})-(\d{4})/g;
const regex4 = /(?<!-)(?<!\d)(1)\s(\d{3})\s(\d{3})\s(\d{4})/g;
const regex5 = /^\d{10}$/g;
const regex6 = /(?<![(])(?<!\d+\s)(\d{3})-(\d{3})-(\d{4})/g;
const regex7 = /(?<!\d)([(]\d{3}[)])(\d{3})-(\d{4})/g
if (regex1.test(input) || regex2.test(input) || regex3.test(input) || regex4.test(input) || regex5.test(input) || regex6.test(input) || regex7.test(input)) {
resultsDiv.innerText = `Valid US number: ${input}`
}
else {
resultsDiv.innerText = `Invalid US number: ${input}`
}
}
clearBtn.addEventListener("click", () => {
resultsDiv.innerText = ""
})
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36
Challenge Information:
Build a Telephone Number Validator Project - Build a Telephone Number Validator
