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

119
Visualizações
Math.random returning same value when console logged javascript

here is my code

// import user input prompt 
var inquirer = require('inquirer');
var emptyFamArray = [];

// ask the user how many relatives do they have

const getQuestions = () => {
    inquirer.prompt([
      {
        name: "relatives_names",
        type: 'input',
        message: "Please input family first names seperated by a space. only 1-10 family members please!",
        
    },
    ]).then((answers => {
        // console.log(answers)
        let test1 = JSON.stringify(answers.relatives_names).split(' ');
        // console.log(test1);
       
        let randomArrayName = test1[Math.floor(test1.length * Math.random())]
        let randomArrayNameTwo = test1[Math.floor(test1.length * Math.random())]
        console.log(randomArrayName)
    
  
        if(test1.length > 10){
            console.log('to many names please input 10 or less')
        } else if (test1.length == 9){

           
            console.log(randomArrayName + " Gets " + randomArrayNameTwo + " for christmas pickings!");
        
         
        }else if (test1.length == 5){
            console.log(randomArrayName + " Gets " + randomArrayNameTwo + " for christmas pickings!");
            console.log(randomArrayName + " Gets " + randomArrayNameTwo + " for christmas pickings!");
            console.log(randomArrayName + " Gets " + randomArrayNameTwo + " for christmas pickings!");
            console.log(randomArrayName + " Gets " + randomArrayNameTwo + " for christmas pickings!");
            console.log(randomArrayName + " Gets " + randomArrayNameTwo + " for christmas pickings!");
          

when it console logs it does randomise the names but it only returns the same value over and over

example

kylee Gets gerald for christmas pickings!
kylee Gets gerald for christmas pickings!
kylee Gets gerald for christmas pickings!

how would I fix this? im stuck and not entirly sure how to make the variables random everytime when logged

Thanks

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

When you are printing to the console the random selection have already happened and it happens only ones before you print out the names. To make it select a random name each time , convert the selection to a function which you will call every time you want to print to console. For example:

var selectRandomName = function(names) {
 return names[Math.floor(names.length * Math.random())]
}

console.log(selectRandomName(names) + " GETS " + selectRandomName(names) + " for Christmas pickings!");

Obviously this is oversimplified and you probably should also prevent selection of the same name twice and other similar cases, but this is out of the scope of current question. I assume this is some sort of exercise, as in real world application you should use some readily available matching libraries.

about 4 years ago · Santiago Gelvez 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