-1

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);
});

blex
  • 24,941
  • 5
  • 39
  • 72
  • How do you think the rng variable is getting updated ?? thats why the infinite loop – rootkonda Feb 21 '20 at 23:22
  • are you doing this in node? – DCR Feb 21 '20 at 23:22
  • The third statement (`Math.floor(Math.random) * HIGH`) of the for loop isn't doing anything. It's not being persisted, so it's just computing and then poofing into the garbage collector. – Taplar Feb 21 '20 at 23:23
  • You also need to call the `Math.random` function: `Math.floor(Math.random() * HIGH)` – Barmar Feb 21 '20 at 23:25

1 Answers1

0

You don't need a loop, you can simply generate a number between LOW and HIGH once:

Math.floor(Math.random() * (HIGH - LOW)) + LOW

But you might want to check if the user did not enter a higher LOW than HIGH:

if (LOW < HIGH) {
  console.log(`Generating a random number between ${LOW} and ${HIGH}...`);
  const rng = Math.floor(Math.random() * (HIGH - LOW)) + LOW;
  console.log(`Your result is: ${rng}`);
} else {
  console.log(`Your LOW number should be lower than your HIGH number.`);
}

Demo including a convertion from String to Number, and a check to see if something else than numbers has been entered:

// Only for the demo to work here
const rl = { question(q, cb) { cb(prompt(q)); } };

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) {
    // Convert these Strings to Numbers
    const LOW = Number(low), HIGH = Number(high);
    // Check if they are numbers (isNaN stands for is Not a Number)
    if (isNaN(LOW) || isNaN(HIGH)) {
      console.log(`You should only enter numbers.`);
    } else if (LOW < HIGH) {
      console.log(`Generating a random number between ${LOW} and ${HIGH}...`);
      const rng = Math.floor(Math.random() * (HIGH - LOW)) + LOW;
      console.log(`Your result is: ${rng}`);
    } else {
      console.log(`Your LOW number should be lower than your HIGH number.`);
    }
  });
});
blex
  • 24,941
  • 5
  • 39
  • 72