Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
Random number generator is giving zero still when minimum and maximum is given
var prevRandomNum = -1;
var arr=[];
var math=[];

window.onload=function(){
    var ref = firebase.database().ref("manga");
ref.once("value", function(snapshot) {
  
    snapshot.forEach(function(childSnapshot) {
    var childKey = childSnapshot.key;
    arr.push(childKey);

});

  }); 
}
function g(){
     const btn = document.getElementById("btng");
    btn.innerText = "Suggest me another";
    var sorry="im/sorry.png";
console.log(arr.length);
    var randomNum;
   var calc=arr.length-1;
if(math.length==calc){
            document.getElementById("mname").textContent= "Sorry you have reached the end of the manga list i have suggested.";
         document.getElementById("mgenre").textContent= "I will be uploading new suggestions soon.";
         document.getElementById("mchap").textContent= "";
         document.getElementById("mstatus").textContent= "";
            document.getElementById("img").src=sorry;
         document.getElementById("pic").style.backgroundImage=`url(${sorry})`;
        
        }
        else{
           do{ 
        randomNum= Math.floor(Math.random() * (arr.length-1) )+1;
        
    
       console.log("rand "+ randomNum);
        
        
        }while(randomNum=== prevRandomNum||math.includes(randomNum));
        
    
    prevRandomNum = randomNum;
math.push(prevRandomNum);
} 
       

Above is the code I am using to retrieve data from firebase database and get value of a specific parent node using random number, i have stored push key in array and i am using random number generator to get a unique and random parent data every time when the button is pressed but the random is giving out 0 as random number even while minimum and maximum number is given. You can check live preview here https://kazemanga.xyz/ please open the console to see if the random number is zero or not. Any help would be appreciated.

arr.length=7 (the no of parent node on db is 7) It may not give random no as Zero on first click so plz refresh and try again, it doesn't give zero sometimes and when I refresh page and try again it gives me.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!