Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

153
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda