+ 7
Random No. Generator (SHOULD BE 4 DIGIT)
Any help would be much appreciated 😄 no.s like 0003 , 0020 , 0400 , 5000 r okay but not 0000
11 Respostas
+ 8
This isn't 4-digit, nor is it in JS, but might help you get an idea of achieving your aims.
https://code.sololearn.com/cuumeZ0dI9iJ/?ref=app
+ 7
I have been trying but I the problem is making it 4 digit
+ 7
num = '""+(1+Math.floor(9999*Math.random()));
while (num.length<4) num = "0"+num;
alert(num);
[edit]
:D
replaced "print" by "alert"... too many Python at same time as JS ^^
+ 6
Your solution require little adaptation: only the most left digit cannot be 0 ;)
+ 6
Yes :)
... but mine solution is more efficient, as it require only one call to random() for generating the number, rather four in yours ;P
+ 6
Thx for all the solution guys I will try them
+ 5
Yes, but you need this only for the most left digit, not the others ^^
+ 4
Well you can tweak this when displaying the number.You could for instance check the length of the generated number. If it is a two digits number, you add two zeros before it. If it is a one digit, you add three zeros before... etc. that's how I personally see it.