0
Hello Sololearners! I tried to make a random number generator and it isn't working.
Can anybody help me fix it? Thanks in advance. https://code.sololearn.com/W78dTNzzy39O/?ref=app
13 Respostas
+ 2
Check out this documentation by MDN
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
It has a good explanation + the answer
+ 3
Check Ja Play 's code, also his explanation is good. I'll add more to it
Your code have this,
var min1 = document.getElementById("min");
Which sets min1 to the #min element, try to log it in your dev tools to see the difference
and you have this
var min2 = parseInt(min1);
Which parses... the element instead of it's value which probably gives a NaN
(think of it as parseInt([HTML Object]))
What you should do is passing the value field association with the element to parseInt
That's the major problem, for the other one try to understand from Ja's code, it's not hard :P
Anyway mention me next time if you really want my help ^^
+ 2
Alexander Shestakov For understanding why your code did not work you have to imagine the code runing. The browser has loaded your code. In this time the inputs min1 and max1 have no any value. After the user done its input and clicked the submit button his values will be not loaded, because in your function this will be not made. The empty inputs will be submited which was created with start of your code.
Have fun with coding.
+ 1
Ja Play tbh those generators have nothing to do in this case lol
+ 1
Seniru thank you for the help
+ 1
Alexander Shestakov some other problems I found in your code are,
- Storing the element as a number instead of it's value
- Using old values inside onclick event, which may give NaNs in future
I'm not sure if that syntax inside onclick attribute works too
+ 1
Ok. First i get you want to code a really generator. The solution of your attempt looks i.e. like as follows:
<body>
<p>Hello! This is a random number generator. You input the min and max numbers and a random one is generated.</p>
<form>
<label>Minimum number:</label>
<input type="number" id="min" required><br>
<label>Maximum number:</label>
<input type="number" id="max" required><br>
</form>
<input type="submit" value="Submit" onclick="document.getElementById('result').innerHTML=GRI()">
<p id="result"></p>
<script>
function GRI(){
var min = document.getElementById("min").value;
var max = document.getElementById("max").value;
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1) ) + min;
}
</script>
</body>
+ 1
Seniru Ja Play Thanks for the help guys. I really appreciate it but can anyone remind me how to get the value because I don't remember.
0
Seniru You are absolutely right
0
Seniru Hi thanks for the help but does anybody know why I am getting NaN-s
0
Seniru Thanks for everything but can you explain me a little bit more please. Thanks in advance.
0
Alexander Shestakov Your welcome.
The full code to the solution has been by me above mentioned. You can copy that and use in your app.
- 1
Here you will find some about generators:
https://www.sololearn.com/learn/JavaScript/2968/