mardi 15 juin 2021

How to check the age from input and perform the appropriate conditional statement?

I'm taking steps back to relearn my foundations on javascript (and realizing that it's a lot shakier than what I've thought). Maybe I just need fresh eyes, but I'm not seeing where the error lies in the code.

( Lot of the things I've read online when I tried to search for answers already had a given age ie. var age = # for the conditionals, but not where it varied based on user input. )

Your help is greatly appreciated! Thank you in advance!

const ageInput = document.querySelector("#ageInput");
const paraContent3 = document.querySelector(".paractice3-p");
const enter = document.querySelector(".enter");

function checkAge() {
  const age = ageInput.innerHTML;
  if (age < 10) {
    paraContent3.innerHTML = "You would roughly be in elementary";
  } else if (age < 14) {
    paraContent3.innerHTML = "You would roughly be in middle school";
  } else if (age < 18) {
    paraContent3.innerHTML = "You would roughly be in high school";
  } else {
    paraContent3.innerHTML = "You're out of school!";
  }
}

enter.addEventListener("keydown", function (e) {
  if (e.target == 13) {
    checkAge();
  }
});
#practice3 {
  display: flex;
  flex-direction: column;
  text-align: center;
}
#practice3 h1 {
  text-transform: uppercase;
}
#practice3 input {
  text-align: center;
  width: 30%;
  position: relative;
  left: 50%;
  transform: translate(-50%);
  padding: 10px;
  text-transform: capitalize;
}
#practice3 .enter {
  text-transform: uppercase;
  width: 20%;
  padding: 10px;
  margin-top: 10px;
  position: relative;
  left: 50%;
  transform: translate(-50%);
}
<section id="practice3">
  <h1>what is your age?</h1>
  <input id="ageInput" type="number" min="1" placeholder="enter your age">
  <p class= "practice3-p"></p>
  <button class="enter">enter</button>
</section>

Aucun commentaire:

Enregistrer un commentaire