0

I'm making a site that assigns you a random number and random start time based on your age and time of registration. a. If you are below 18 and you registered before 7:30 AM, you will be assigned a random number above 1000 and you will start at 9:30 AM. b. If you are below 18 and you registered from 7:31 AM to 9:00 AM, you will get a random number below 1000 and you will start at 11:00 AM. c. If you are below 18 and you registered from 9:01 AM onwards, you will get a random number below 1000 and you will start at 12:30 AM.

Can I use if else statements to compare the times of registration submitted by the users (ex. registrationTime > 7:30)? If not, is there another way?

<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="style.css">
    <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>Race Day</title>
</head>
<body>
    <header>
        <h1>Race Day</h1>
    </header>
    <main>
        <div class="img-wrapper">
        <img src="http://pixelartmaker-data-78746291193.nyc3.digitaloceanspaces.com/image/486628a7f81e711.png" alt="runner">
        </div>
        <div class="form-wrapper" id="form-wrapper">
        <form action="#">
            <label for="fullName">Name</label>
            <input type="text" name="fullName" id="fullName" autocomplete="off" required>
            <label for="age">Age</label>
            <input type="number" name="age" id="age" min="1" max="100" required>
            <label for="time">Time of Registration</label>
            <input type="time" name="time" id="time" required>
            <button id="myBtn" type="button" onclick="save()">Submit</button>
        </form>
    </div>
        <div class="raceNumberContainer">
            <p id="greeting"></p>
            <h1 id="racenumber"></h1>
            <p id="startTime"></p>
        </div>
    </main>
    <script>
        // initialize variables

        // saving the input values
        function save() {
            let nameValue = document.getElementById('fullName').value
            let ageValue = document.getElementById('age').value
            let timeValue = document.getElementById('time').value
            console.log(`Name: ${nameValue} Age: ${ageValue} Time of Registration: ${timeValue}`);

            // getting the variables for display
            let greetingDisplay = document.getElementById('greeting');
            let raceNumberDisplay = document.getElementById('racenumber');
            let startTimeDisplay = document.getElementById('startTime');

            // LOGIC FOR THE NUMBER ASSIGNMENT
            let raceNumber = Math.floor(Math.random() * 1000);
            

            // specify the text in the greeting element
            let greetingText = `Hello, ${nameValue}! Your race number is`;

            // display the values entered by the user
            greetingDisplay.innerText = greetingText;
            raceNumberDisplay.innerText = 4;
            startTimeDisplay.innerText = "5:40 PM";
        }  
    
    </script>
</body>
</html>
Youssouf Oumar
  • 29,373
  • 11
  • 46
  • 65
smolbunny
  • 55
  • 3
  • What is your actual problem? The flippant way to answer your question is "yes, you can use if else statements", but I'm confident that's not what you're actually looking for. – possum May 23 '22 at 12:20
  • according to MDN "The value of the time input is always in 24-hour format that includes leading zeros: hh:mm, regardless of the input format". with that considered, you can simply get rid of the semicolon and parse the value as integer, like `parseInt("14:16".replace(':',''))` that will result in the integer number `1416` which can be compared to any other time value formatted in the same way – GrafiCode May 23 '22 at 12:22
  • 1
    in your example, "registrationTime > 7:30", if I register now it would be: `if ( parseInt("14:26".replace(':','')) > parseInt("07:30".replace(':','')) )` this "translates" into `if (1426 > 730)` – GrafiCode May 23 '22 at 12:26
  • 1
    You _can_ compare date or time values in _string_ form, if all the parts are "same-width" in each of the two values, and properly ordered - because string comparison happens character by character, from left to right. So you can "compare" f.e. `2022-01-05` and `2022-02-15`, or `07:30` and `12:15`, using `==`, `>=` or `<=`. It will of course not work to compare `7:30` to `12:15`, and not for `15.02.2022` vs `05.01.2022` either. – CBroe May 23 '22 at 12:28

0 Answers0