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

118
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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