0

I trying to introduce a loop For, to read 5 olympic athletes and return your category according to your age, but i don't know how to read the array in the loop, i'm still learning how to work with it

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Categoria Olimpíadas</title>
    <input id="numberInput" name="numberInput" value="" type="array" />
    <button onclick="send()">Confirm</button>

    <script type="text/javascript">
      function send() {
        const age = document.getElementById("numberInput").value;
        const numbers = [age];

        console.log(numbers);
        for (i = 0; i <= 5; i++) {
          if (numbers <= 12) {
            alert("Not qualified to compete");
          } else if (13 >= numbers || numbers <= 15) {
            alert("Minor category");
          } else if (16 >= numbers || numbers <= 19) {
            alert("Juvenile category");
          } else if (20 >= numbers || numbers <= 23) {
            alert("Under 23 category");
          } else if (24 >= numbers || numbers <= 35) {
            alert("Adult category");
          } else {
            alert("Master category");
          }
        }
      }
    </script>
  </head>
</html>
  • ` for (let i = 0; i <= 5; i++) {` – Chris G Mar 20 '23 at 12:33
  • Did you mean adding 5 different number and then get alert according to it? – Nisha Mar 20 '23 at 12:37
  • 1
    Does this answer your question? [Loop through an array in JavaScript](https://stackoverflow.com/questions/3010840/loop-through-an-array-in-javascript) – ponury-kostek Mar 20 '23 at 12:44
  • There is no reason to check for the lower bounds. `13 >= numbers ||` is meaningless when the previous else if would have the match. – epascarello Mar 20 '23 at 12:53
  • what is supposed to be/do? To my knowledge "array" is not an available type for HTML input elements... Why are you trying to get the inputted age as an array to begin with, what is the thought behind that - why not just use the integer... The loop as is just executes the same code 6 times, there is nothing that changes between iterations, so why is there a loop to begin with? – Lord-JulianXLII Mar 20 '23 at 14:16

1 Answers1

0

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Categoria Olimpíadas</title>
  </head>
  <body>
    <label for="ageInput">Enter athlete's age:</label>
    <input id="ageInput" name="ageInput" value="" type="number" />
    <button onclick="send()">Confirm</button>

    <script type="text/javascript">
      function send() {
        const ageInput = document.getElementById("ageInput");
        const age = parseInt(ageInput.value);
        const categories = [
          "Not qualified to compete",
          "Minor category",
          "Juvenile category",
          "Under 23 category",
          "Adult category",
          "Master category",
        ];

        if (age <= 12) {
          alert(categories[0]);
        } else if (age >= 13 && age <= 15) {
          alert(categories[1]);
        } else if (age >= 16 && age <= 19) {
          alert(categories[2]);
        } else if (age >= 20 && age <= 23) {
          alert(categories[3]);
        } else if (age >= 24 && age <= 35) {
          alert(categories[4]);
        } else {
          alert(categories[5]);
        }

       
        ageInput.value = "";
      }
    </script>
  </body>
</html>

check this. I think it would be helpful