Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

120
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!