Author

Topic: Simple javascript function (Read 585 times)

newbie
Activity: 15
Merit: 0
January 07, 2017, 08:46:21 PM
#9


   
      Random Number
   
   
      

      
   


Save this as whatever.html
legendary
Activity: 1442
Merit: 1186
January 07, 2017, 08:42:34 PM
#8

for (var i=1;i<5;i--) {
  ...
}


This is an infinite loop, not good.

Create a div that takes up the whole page with CSS and give it an overflow-wrap property of break-word

Code:





Then write a simple loop that adds each new random number to the end of the string enough times to fill the page, but not infinitely.

Code:
var filler = "";
for(i=0;i<3500;i++){
  var num = Math.floor(Math.random() * 9) + 1;
  filler += num;
  }
document.getElementById("numbers").innerHTML = filler;

https://jsfiddle.net/4qwercqu/

sr. member
Activity: 364
Merit: 250
January 07, 2017, 04:08:12 PM
#7
Draft:













Random number javascript









Tried putting the script both at the top and the bottom but no result.

Please remove the JS code from the head tag and view the console output to see if you can find something Wink
hero member
Activity: 976
Merit: 1002
Bitcoinmeetups.org
January 07, 2017, 04:06:46 PM
#6
Draft:













Random number javascript









Tried putting the script both at the top and the bottom but no result.
hero member
Activity: 976
Merit: 1002
Bitcoinmeetups.org
January 07, 2017, 04:00:01 PM
#5
Ok. Going to bed now but will try to test it as soon as possible and provide some feedback maybe tomorrow or the day after. See you later moritz30.
sr. member
Activity: 364
Merit: 250
January 07, 2017, 03:38:35 PM
#4
document.write(Math.floor((Math.random() * 9) + 1)); It displays a random number between 1 and 9

Ah, yes, I think I have that snippet in my code library somewhere. Can you put it in a loop please? I can probably do it myself but you are probably faster.

Not that way because it would always reload the page. You can try the following:

HTML:



JS:
for (var i=1;i<5;i--) {
   document.getElementById('abc').innerHTML = document.getElementById('abc').innerHTML + "" + Math.floor((Math.random() * 9) + 1);
}
hero member
Activity: 976
Merit: 1002
Bitcoinmeetups.org
January 07, 2017, 03:34:44 PM
#3
document.write(Math.floor((Math.random() * 9) + 1)); It displays a random number between 1 and 9

Ah, yes, I think I have that snippet in my code library somewhere. Can you put it in a loop please? I can probably do it myself but you are probably faster.
sr. member
Activity: 364
Merit: 250
January 07, 2017, 03:30:03 PM
#2
document.write(Math.floor((Math.random() * 9) + 1)); It displays a random number between 1 and 9
hero member
Activity: 976
Merit: 1002
Bitcoinmeetups.org
January 07, 2017, 03:27:38 PM
#1
Hello,

I would like to code a simple javascript function that makes the whole website display random numbers between 0-9. Shouldn't be more than a few lines of code.

Example:

1345349085234985234908752864748596793845769834576983457698746507293467239486928 4569084756903847569038475690834756983475698374569873459687345689735468975489762 -984567289457698457698347569834756

And so on.

Anyone can post a snippet?

Also, if you want to discuss some development ideas you can call me on Facebook.

Bmeet

Please note it is just for fun and mostly creative purposes for now and I am not paying anything at the moment.
Jump to: