+ 1
+"" you got it!
12th Mar 2022, 2:53 AM
Bob_Li
Bob_Li - avatar
+ 2
convert number to string by concatenating the number to an empty string. it's a pair of double quotes "", but you can also use a pair of single quotes ''. you can use toString() too, but I'm lazy... using string templates seems to be faster, though... https://dev.to/sanchithasr/5-ways-to-convert-number-to-string-3fhd https://i.stack.imgur.com/mPxVd.png
12th Mar 2022, 2:05 AM
Bob_Li
Bob_Li - avatar
+ 1
Swap second and first. Also tell the user what they are entering ie min or max
11th Mar 2022, 1:53 AM
William Owens
William Owens - avatar
+ 1
To generate random numbers within a range use a function that takes two arguments(max, min) and return Math.random(max-min) + min
11th Mar 2022, 5:54 PM
TrUtH
TrUtH - avatar
+ 1
Your Mom function convert() { let min = ~~document.getElementById("num1").value; let max = ~~document.getElementById("num2").value; min = Math.ceil(min); max = Math.floor(max); document.getElementById("demo").innerHTML = Math.floor(Math.random() * (max - min + 1)) + min + ""; } converting to number(using ~~) and converting to string(using +"") seems to make the button click more responsive, too.
11th Mar 2022, 11:00 PM
Bob_Li
Bob_Li - avatar
+ 1
~~ is a risky shortcut to convert to number. Probably better to use Number(....), but Number returns a Nan for invalid inputs while ~~ converts it to 0. Javascript will convert values into strings when doing string concatenation, so + '' (an empty string) will convert the number to string. Might be a good idea to use type="number" for the inputs to prevent invalid string inputs. https://stackoverflow.com/questions/4055633/what-does-double-tilde-do-in-javascript
11th Mar 2022, 11:09 PM
Bob_Li
Bob_Li - avatar
+ 1
Sajjad Cheraghi with out the +1 in (max-min) the max is not inclusive. Remember math.random always produces a number less than 1 not one inclusive.
13th Mar 2022, 1:31 PM
William Owens
William Owens - avatar
0
Math.random always returns a number less than 1 you have to make an adjustment to the random math.floor(math.round() * ((max - min + 1) + min)
11th Mar 2022, 1:16 AM
William Owens
William Owens - avatar
11th Mar 2022, 1:27 AM
William Owens
William Owens - avatar
0
Yes you will still use math.floor as well so you can round the decimal that math.round creates. You will then * by the range you want. math.floor(math.round() * 100) Is random 0 to 99.
11th Mar 2022, 1:31 AM
William Owens
William Owens - avatar
0
Look at your math.round() It needs to be: math.round() * ((max - min + 1) + min) You have: math.random() * max, min
11th Mar 2022, 1:38 AM
William Owens
William Owens - avatar
0
Almost you have one ) in the wrong spot. Look at (max - min +1) + min
11th Mar 2022, 1:45 AM
William Owens
William Owens - avatar
0
Let's build it: math.floor() math.floor(math.round()) math.floor(math.round() * ()) math.floor(math.round() * (())) math.floor(math.round() * ((max - min + 1) + min))
11th Mar 2022, 1:49 AM
William Owens
William Owens - avatar
0
Awesome have fun see ya around.
11th Mar 2022, 1:56 AM
William Owens
William Owens - avatar
0
Your Mom put the entire thing inside the function so it updates everytime the button is clicked. Also, you want the value, not the input element. function convert() { let firstNumber = document.getElementById("num1").value; let secondNumber = document.getElementById("num2").value; let result = Math.floor(Math.random() * (secondNumber - firstNumber + 1) + firstNumber); document.getElementById("demo").innerHTML = result; }
11th Mar 2022, 6:08 AM
Bob_Li
Bob_Li - avatar
0
yes, and maybe it is gibberish now, but it will make sense as you learn more. Keep learning!
12th Mar 2022, 2:21 AM
Bob_Li
Bob_Li - avatar
0
well, the innerHTML is expecting a string.
12th Mar 2022, 2:25 AM
Bob_Li
Bob_Li - avatar
0
a number is well, a number. like 1 a string is a string, like "1" the value of an input element is a string, even if you use type="number". you need numbers for calculation, so you convert the string to number. your innerHTML expects a string, so you convert the result back to string. types are really loosely implemented in javascript. So it would probably not choke if you mess those up, ...probably... . But type correctness is something to watch out for.
12th Mar 2022, 2:29 AM
Bob_Li
Bob_Li - avatar
0
but in this case we are mis-using it to convert string to number string --> number ~~"3" becomes 3
12th Mar 2022, 2:49 AM
Bob_Li
Bob_Li - avatar
0
use console.log experiment... its -4
12th Mar 2022, 2:56 AM
Bob_Li
Bob_Li - avatar