Bitcoin Forum
October 15, 2019, 12:51:18 AM *
News: Latest Bitcoin Core release: 0.18.1 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Simple javascript function  (Read 485 times)
This is a self-moderated topic. If you do not want to be moderated by the person who started this topic, create a new topic.
Bitcoinmeetups.org
Hero Member
*****
Offline Offline

Activity: 933
Merit: 1000


Bitcoinmeetups.org


View Profile WWW
January 07, 2017, 08:27:38 PM
Last edit: January 07, 2017, 08:38:05 PM by Bitcoinmeetups.org
 #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.

Bitcoinmeetups.org: Creative, Blockchain and AI Project Development.
Telegram: Apply to join our group at: https://t.me/joinchat/KU7ltBBaG59499AJ0Az4nw
Web: http://www.bitcoinmeetups.org
1571100678
Hero Member
*
Offline Offline

Posts: 1571100678

View Profile Personal Message (Offline)

Ignore
1571100678
Reply with quote  #2

1571100678
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1571100678
Hero Member
*
Offline Offline

Posts: 1571100678

View Profile Personal Message (Offline)

Ignore
1571100678
Reply with quote  #2

1571100678
Report to moderator
1571100678
Hero Member
*
Offline Offline

Posts: 1571100678

View Profile Personal Message (Offline)

Ignore
1571100678
Reply with quote  #2

1571100678
Report to moderator
Moritz30
Sr. Member
****
Offline Offline

Activity: 378
Merit: 250


View Profile
January 07, 2017, 08:30:03 PM
 #2

document.write(Math.floor((Math.random() * 9) + 1)); It displays a random number between 1 and 9
Bitcoinmeetups.org
Hero Member
*****
Offline Offline

Activity: 933
Merit: 1000


Bitcoinmeetups.org


View Profile WWW
January 07, 2017, 08: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.

Bitcoinmeetups.org: Creative, Blockchain and AI Project Development.
Telegram: Apply to join our group at: https://t.me/joinchat/KU7ltBBaG59499AJ0Az4nw
Web: http://www.bitcoinmeetups.org
Moritz30
Sr. Member
****
Offline Offline

Activity: 378
Merit: 250


View Profile
January 07, 2017, 08: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:

<p id="abc"></p>

JS:
for (var i=1;i<5;i--) {
   document.getElementById('abc').innerHTML = document.getElementById('abc').innerHTML + "" + Math.floor((Math.random() * 9) + 1);
}
Bitcoinmeetups.org
Hero Member
*****
Offline Offline

Activity: 933
Merit: 1000


Bitcoinmeetups.org


View Profile WWW
January 07, 2017, 09: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.

Bitcoinmeetups.org: Creative, Blockchain and AI Project Development.
Telegram: Apply to join our group at: https://t.me/joinchat/KU7ltBBaG59499AJ0Az4nw
Web: http://www.bitcoinmeetups.org
Bitcoinmeetups.org
Hero Member
*****
Offline Offline

Activity: 933
Merit: 1000


Bitcoinmeetups.org


View Profile WWW
January 07, 2017, 09:06:46 PM
 #6

Draft:

<html>

<head>

<script type="text/javascript">

for (var i=1;i<5;i--) {
   document.getElementById('abc').innerHTML = document.getElementById('abc').innerHTML + "" + Math.floor((Math.random() * 9) + 1);
}

</script>


</head>


<body>

Random number javascript

<p id="abc"></p>


<script type="text/javascript">

for (var i=1;i<5;i--) {
   document.getElementById('abc').innerHTML = document.getElementById('abc').innerHTML + "" + Math.floor((Math.random() * 9) + 1);
}

</script>


</body>

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

Bitcoinmeetups.org: Creative, Blockchain and AI Project Development.
Telegram: Apply to join our group at: https://t.me/joinchat/KU7ltBBaG59499AJ0Az4nw
Web: http://www.bitcoinmeetups.org
Moritz30
Sr. Member
****
Offline Offline

Activity: 378
Merit: 250


View Profile
January 07, 2017, 09:08:12 PM
 #7

Draft:

<html>

<head>

<script type="text/javascript">

for (var i=1;i<5;i--) {
   document.getElementById('abc').innerHTML = document.getElementById('abc').innerHTML + "" + Math.floor((Math.random() * 9) + 1);
}

</script>


</head>


<body>

Random number javascript

<p id="abc"></p>


<script type="text/javascript">

for (var i=1;i<5;i--) {
   document.getElementById('abc').innerHTML = document.getElementById('abc').innerHTML + "" + Math.floor((Math.random() * 9) + 1);
}

</script>


</body>

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
coinableS
Legendary
*
Offline Offline

Activity: 1092
Merit: 1001



View Profile WWW
January 08, 2017, 01:42:34 AM
 #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:
<head>
<style>
#numbers{
  width: 100%;
  height: 900px;
  background-color: #aeaeae;
  overflow-wrap: break-word;
}
</style>
</head>
<div id="numbers"></div>

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/


TheBitcoinMiner
Newbie
*
Offline Offline

Activity: 15
Merit: 0


View Profile
January 08, 2017, 01:46:21 AM
 #9

<!DOCTYPE html>
<html>
   <head>
      <title>Random Number</title>
   </head>
   <body>
      <div id="abc"></div>
      <script type="text/javascript">
         var abc = document.getElementById("abc");
         //Change the 10 to the number of number you want to generate
         for (var i = 0; i < 10; i++) {
            abc.innerHTML += Math.floor(Math.random()*10).toString();
         }
      </script>
   </body>
</html>

Save this as whatever.html
Pages: [1]
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!