dimanche 5 avril 2020

Is there a solution to "Invalid Value"?

It seems like I can't find why it shows EVERYTIME I type in something. It's supposed to only shows when user types in letter or negative numbers. Does it how something to do with "else"?

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Examus</title>
</head>

<body>
  <h1 id="label"></h1>

  <input type="text" id="input">
  <button onclick="checkAge()">Check Age</button>


  <script>
    const checkAge = () => {
      const input = document.getElementById("input");
      const inputValue = parseInt(input.value);

      let output;

      if (Number.isInteger(inputValue) || inputValue < 0) {
        output = "Invalid Value";
        document.getElementById("label").innerText = output;

        return;
      }

      if (inputValue < 14 && inputValue > 0) {
        output = "This person is a KID";
      } else if (inputValue > 14 && inputValue < 18) {
        output = "This person is a TEEN";
      } else if (inputValue > 18 && inputValue < 60) {
        output = "This person is a ADULT";
      } else if (inputValue > 60) {
        output = "This person is a SENIOR";
      } else {
        output = "Invalid Value";
      }

      document.getElementById("label").innerText = output;
    }
  </script>
</body>

</html>

Aucun commentaire:

Enregistrer un commentaire