Random Number Generator

I’ve been trying to make a customizable random number generator, if you set the minimum to 20 with the maximum of 26 it adds the numbers weirdly. Rather than adding the numbers together, it combines it so the addition that makes it work better gets put on the end of the output. Does anyone know how to fix this?

Here is the file:
Random Number Generator W.I.P.wick (26.1 KB)

1 Like

These are the lines in your code where the minimum value is being set by the user:

let min = prompt("Enter a Minimum Value:");
Min=min;

You used the “prompt” function to set the minimum value.
Since prompt() returns a string value,Min” becomes set to a string rather than a number.

When you add up a string with another variable, you get a string in return.

Example:

alert("ABC"+(23+10)) // output: "ABC33"
alert("ABC"+23+10) // output: "ABC2310"

There are different ways to change a string into a number.
The simplest is probably using the Number("...") function.

alert(Number("10")+23+10) // output: 43

The method that I like using is multiplying the string by 1.

alert(("10"*1)+23+10) // output: 43

^ I only prefer this method bc it’s less typing

Either way works.

For now, try using this in your project:

let min = Number(prompt("Enter a Minimum Value:"));
Min=min;

Also, if a user types letters or something other than a number in the prompt, the minimum value will be set to NaN (Not a Number).

Here's how to fix that
let min = Number(prompt("Enter a Minimum Value:"));
if(!isNaN(min))
Min=min;

Let me know if it works

2 Likes

Thank you that was very helpful!

1 Like