vendredi 21 février 2020

Infinitely repeating for loop [duplicate]

I've been trying to create a simple Random Number Generator as a way to apply some of what I've learned with JavaScript, and it seems to be repeating a certain value infinitely.

Its main purpose, of course, is to randomly generate a number between a HIGH[est] and LOW[est] value that the user can input from the command prompt on Windows.

But what it's instead doing is infinitely repeating the LOW variable. So far, I've tried adding break; in multiple places, to no avail. I'm not returning any specific errors, either.

What I think the problem is, is that I've messed up in the 3 statements contained as arguments (?) for the loop. I'm not entirely sure though, so I'd like some help. Please excuse my illiteracy with the terminology of all this, I'm just now starting to figure all of this out.

Here's the code:

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.question("What number would you like to set to your LOW value? ", function(LOW) {
    rl.question("What number would you like to assign to the HIGH? ", function(HIGH) {
        console.log(`Generating a random number between ${LOW} and ${HIGH}...`);
        for(let rng = LOW; rng < HIGH; Math.floor(Math.random) * HIGH) {
            if (rng < LOW) {
                console.log("Arithmetic err")
                break;
            } else if (rng > HIGH) {
                console.log("Arithmetic err")
                break;
            }
        };
        console.log(`Your result is: ${rng}`); break;
    });
});

rl.on("close", function() { 
    console.log("\nDone!")
    process.exit(0);
});




Aucun commentaire:

Enregistrer un commentaire