Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Random Number generate

I want to generate random number between 0 to 100 and show a div tag if that random isn't less than 10. I have this code but it doesn't work, please can you help me fix the error.

I have already tried on google but I can not find code for php

function showbox(){
    document.getElementById("ap1").style.visibility = "visible";
}

function myFunction(name) {
    var x = document.getElementById("demo")
    x.innerHTML = Math.floor((Math.random() * 100) + 1);
}

if(myFunction(<=10){
    setTimeout(showbox, 35000);
}
<div id="ap1" style="visibility: hidden;"></div>
like image 542
Shanmuganathan Shanshayan Avatar asked Sep 16 '26 00:09

Shanmuganathan Shanshayan


1 Answers

Your code has SyntaxError. You can use this code

var element = document.getElementById("ap1");
var random = Math.floor((Math.random() * 100) + 1);
element.innerHTML = random;

if (random >= 10){
    document.getElementById("ap1").style.visibility = "visible";   
} else
  console.log(random);
<div id="ap1" style="visibility: hidden;"></div>

Math.floor((Math.random() * 100) + 1) generate number between 1 and 100. If you want to generate number between 0 and 100 use Math.floor((Math.random() * 101)) or Math.round((Math.random() * 100))

like image 64
Mohammad Avatar answered Sep 19 '26 20:09

Mohammad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!