Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

152
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda