Build a Telephone Number Validator Project - Build a Telephone Number Validator

Tell us what’s happening:

I was having problem with variable definiton, and reading other threads on forum I found that it was a bug or someting with the tests.
So I use the variables that @ILM recomendeded in other post.
But now, the user storie 35 still not passing. Any sugestion?

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Telephone number Checker</title>
    <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    <h1>
      Telephone number <br />
      Checker
    </h1>
    <section class="input-container">
      <label for="number-input">Enter a telephone number:</label>
      <input
        id="user-input"
        class="number-input"
      />
      <button class="convert-btn" id="check-btn">Check</button>
    </section>
    <section class="output-container">
      <h2>Check History</h2>
      <div id="results-div"></div>
      <button class="convert-btn" id="clear-btn">Clear</button>
    </section>
    <script src="script.js"></script>
  </body>
</html>

/* file: script.js */
const userInput = document.getElementById("user-input")
const resultsDiv = document.getElementById("results-div")
const checkBtn = document.getElementById("check-btn")
const clearBtn = document.getElementById("clear-btn")


const check = () =>{
  if(!userInput.value){
    alert("Please provide a phone number")
    return
  }
  //regular expressions for the valid phone number exemples
  const validExemples = [/^\d{3}\-\d{3}\-\d{4}$/,
                  /^\(\d{3}\)\d{3}\-\d{4}$/,
                  /^\(\d{3}\)\s\d{3}\-\d{4}$/,
                  /^\(\d{3}\)\s\d{3}\s\d{4}$/,
                  /^\d{10}$/,
                  /^1\s\d{3}\s\d{3}\s\d{4}$/,
                  /^1\s\(\d{3}\)\s\d{3}\-\d{4}$/,
                  /^1\(\d{3}\)\d{3}\-\d{4}$/,
                  /^1\s\d{3}\-\d{3}\-\d{4}$/]

  for (let exemple of validExemples){
    if (exemple.test(userInput.value)){
      addResult(`Valid US number: ${userInput.value}`)
      userInput.value = ""
      return
    }
  }
  addResult(`Invalid US number: ${userInput.value}`)
  userInput.value = ""
  return
}


const addResult = (text) =>{
  resultsDiv.innerHTML += `${text} <br>`
}

const clear = () =>{
  resultsDiv.innerText = ""
}

clearBtn.addEventListener("click", clear)
checkBtn.addEventListener("click", check)
/* file: styles.css */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --light-grey: #f5f6f7;
  --dark-blue: #1b1b32;
  --orange: #f1be32;
}

body {
  display: flex;
  flex-direction: column;
  align-items: center;
  font-family: Menlo, Consolas, Monaco, Liberation Mono, Lucida Console,
    monospace;
  font-size: 1.125rem;
  color: var(--light-grey);
  background-color: var(--dark-blue);
  padding: 0 4px;
}

h1 {
  font-size: 2.125rem;
  text-align: center;
  margin: 20px 0;
}

h2 {
  font-size: 1.5rem;
  text-align: center;
  margin: 20px 0;
}

.input-container {
  display: flex;
  flex-direction: column;
  gap: 10px;
  justify-content: center;
  align-items: center;
  width: clamp(320px, 50vw, 460px);
  margin: 10px auto;
}

.input-container label {
  white-space: nowrap;
  word-spacing: -6px;
}

.convert-btn {
  font-size: inherit;
  font-family: inherit;
  background-color: var(--orange);
  width: 100%;
  height: 2rem;
  padding: 0 6px;
  border: none;
  border-radius: 2px;
  cursor: pointer;
}

.number-input {
  font-size: inherit;
  padding: 0.3rem;
  width: 100%;
}

.output-container {
  margin-inline: auto;
  width: clamp(320px, 50vw, 460px);
}


#results-div {
  display: flex;
  flex-direction: column-reverse;
  justify-content: end;
  gap: 1rem;
  margin-block-end: 1rem;
  min-height: 40vh;
  border: 2px dashed var(--orange);
  padding: 1rem;
}


@media screen and (min-width: 36em) {
  body {
    font-size: 1rem;
  }

  .input-container {
    flex-direction: row;
    width: unset;
  }

  .number-input {
    width: unset;
  }
}

The console output:

// running tests
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.
// tests completed

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36

Challenge Information:

Build a Telephone Number Validator Project - Build a Telephone Number Validator

There’s a bug in the test and you’ve applied the workaround but it didn’t work.

You don’t really know what the bug is, or what the fix for it is or how that might interact with your code. You’re trying to troubleshoot without a full picture of the problem and trying to guess at something that might pass the test with a known bug.

I would circle back on this once the fix is implemented and try it again, and spend your time on the next problem ahead, if I were you.

1 Like

The issue is not the bug, the bug only issue is having undeclared variables, which you have overcome. So now the issue is your code.

I have added a console.log in your code to check what numbers are tested, and what is your code evaluation

{ tested: '1 775-232-4572' }
Valid
{ tested: '1 (462)857-8834' }
Invalid
{ tested: '10 904-923-5840' }
Invalid
{ tested: '1 (10)848-4927' }
Invalid
{ tested: '1!(908)992-7392' }
Invalid
{ tested: '-1 666 351 1767' }
Invalid
{ tested: '24149404' }
Invalid
{ tested: '913#547-5967' }
Invalid
{ tested: '(133650-6021' }
Invalid

While for most of them the evaluation seems correct, I have doubts for this one:

{ tested: '1 (462)857-8834' }
Invalid

isn’t this a valid number?

running the tests again, an other one: (the numbers for 35 and 36 are generated randomly each time)

{ tested: '1 (725)352-5274' }
Invalid

I think your code doesn’t like this format

2 Likes

Thank you, very much!
I make the regex based in the exemples… this format isn’t in the exemples… =(
I already make the correction, and it’s worked!

Do you think I need to remove the code from the post, to not give this answer to another students? haha

1 Like