vendredi 4 décembre 2020

Why is my javascript for loop not doing anything and instead freezing up my webpage?

I'm having trouble figuring out why my code won't work. It's a really simple program so I'm sure there is just something I'm missing or not seeing but I can't seem to figure it out. Please help!

What it's supposed to do: take in two numbers and find the next number that's greater than the first number AND is also divisible by the second number.

What's happening: it's basically just freezing up my computer, it seems like the loop just continues to run without stopping.

JS:

function divisibleBy(first, second) {
    var firstNum = document.getElementById('firstNumber').value;
    var secondNum = document.getElementById('secondNumber').value;

    for (var i = firstNum + 1; i > firstNum; i++) {
        if (i % secondNum === 0) {
            document.getElementById('result').innerHTML = i;
        }
    }
}

HTML:

<body>
    <header>
        <h1>Let's Do Some Math!</h1>
        <h2>Type in <u>two</u> numbers and let's figure out the next <br>
            number greater than those two numbers but with a catch...<br>
            the number has to be <u>divisible</u> by the second number you choose.</h2>
    </header>
    <hr>
    <ul>
        <li>
            <label for="firstNumber">First Number:</label>
            <input type="number" id="firstNumber">
        </li>
        <li>
            <label for="secondNumber">Second Number:</label>
            <input type="number" id="secondNumber">
        </li>
        <li>
            <button type="submit" onclick="divisibleBy();">GO</button>
        </li>
    </ul>

    <h2 id="result"></h2>
    <hr>
</body>

Aucun commentaire:

Enregistrer un commentaire