2021-12-11

Tech Programing

程式人小天地

Math.random returning same value when console logged javascript

1 min read


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



Source link

資料來源:Stackoverflow

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *