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