Bitcoin Forum

Bitcoin => Project Development => Topic started by: Bitcoinmeetups.org on January 07, 2017, 08:27:38 PM



Title: Simple javascript function
Post by: Bitcoinmeetups.org on January 07, 2017, 08:27:38 PM
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.


Title: Re: Simple javascript function
Post by: Moritz30 on January 07, 2017, 08:30:03 PM
document.write(Math.floor((Math.random() * 9) + 1)); It displays a random number between 1 and 9


Title: Re: Simple javascript function
Post by: Bitcoinmeetups.org on January 07, 2017, 08:34:44 PM
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.


Title: Re: Simple javascript function
Post by: Moritz30 on January 07, 2017, 08:38:35 PM
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);
}


Title: Re: Simple javascript function
Post by: Bitcoinmeetups.org on January 07, 2017, 09:00:01 PM
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.


Title: Re: Simple javascript function
Post by: Bitcoinmeetups.org on January 07, 2017, 09:06:46 PM
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.


Title: Re: Simple javascript function
Post by: Moritz30 on January 07, 2017, 09:08:12 PM
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 ;)


Title: Re: Simple javascript function
Post by: coinableS on January 08, 2017, 01:42:34 AM

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/



Title: Re: Simple javascript function
Post by: TheBitcoinMiner on January 08, 2017, 01:46:21 AM
<!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