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

172
Visualizações
Javascript how to randomly select a specific amount of objects?

I have created a function that returns the login names of random users located on the following api website(https://api.github.com/users)

<script>
fetch('https://api.github.com/users').then(function(response) {
 
    response.json().then(function(users){
    document.write("Highlighred Github users")
    users.forEach(function(user){ 

for (let i=0; i< 8; i++) {
   document.write('<br>')
   document.write((users[Math.floor(Math.random() *users.length)].login))
}
    });
  });
}).catch(err => console.error(err));
</script>

Currently it does return random login names from that file, however, I wish to only randomly select 8 users from this list. My attempt at doing so can be seen in the for loop i < 8 however it prints alot more than 8(240 to be exact)

How can I have it so just returns 8 randomly? say for example

ezmobius
mojodna
kevwil
wayneeseguin
railsjitsu
bmizerany
atmos
fanvsfan
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

please, remove useless loop, i have commented out users.forEach(function(user){

fetch('https://api.github.com/users').then(function(response) {
 
    response.json().then(function(users){
    document.write("Highlighred Github users")
//     users.forEach(function(user){ 

for (let i=0; i< 8; i++) {
   document.write('<br>')
   document.write((users[Math.floor(Math.random() *users.length)].login))
}
    });
//   });
}).catch(err => console.error(err));

about 4 years ago · Juan Pablo Isaza Relatório
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