1

I'm trying a project that will choose a random answer out of one of the 3 nested arrays in my object. My goal is to know afterwards out of which array the answer came, so I can use that to know what the new sentence will be.

The example below works with 1 array, but i want to know if the answer is a good, bad or doubt answer, hence why i put it in an object.

EDIT: code below gives me a random property in my object, but i need to go one step deeper. The comment next to it is what i tried but it gives me undefined.

const randomAnswers = {
goodAnswers: ['yes','Absolutely','certainly',"I'm sure",'HELL YES'],
maybes: ['maybe','probably','perhaps',"honestly man, i really don't 
know this one"],
badAnswers: ['LOL NO','no way man','maybe',"forget it",'HELL no','Are 
you serious?']
};
{
const init = () => {
console.log("initiated");
let answer = document.querySelector('.answer');
answer.style.opacity = 0;
setTimeout(fixSentence,3000);
//fixSentence();
}
const fixSentence = () => {
 let a = document.querySelector('.answer');
 let think = document.querySelector('.think');
 console.log('shown');
 console.log(a.textContent);
 let randomAnswer = randomAnswers[Math.floor(Math.random()*randomAnswers.length)];
 var randomProperty = function (obj) {
 var keys = Object.keys(obj)
 let random = obj[keys[ keys.length * Math.random() << 0]];
 return random/*[Math.floor(Math.random()*randomAnswers.length)]*/;
 };
 console.log(randomProperty(randomAnswers))
 let splittedSentence = a.textContent.split(" ");
 console.log(splittedSentence);
 a.textContent = `${randomAnswer}`;
 a.style.opacity = 1;
 think.style.opacity = 0;
}
init();

}

the output of console.log(randomAnswer) is obviously undefined right now, but I can't figure out how to choose a random item out of one of the three arrays in the object.

asked Jun 9, 2019 at 13:21
2
  • One question is what probability you want? Want to have a higher probability for bad answer just because you have more ways of giving a bad answer? Or do you want the same probability (1/3) to have a good, maybe or bad answer? Commented Jun 9, 2019 at 13:35
  • Actually, it would be nice if it wouldn't pick the same array 2 times after eachother. Commented Jun 9, 2019 at 13:38

1 Answer 1

2

You could do something like this:

const randomAnswers = {
 goodAnswers: ['yes','Absolutely','certainly',"I'm sure",'HELL YES'],
 maybes: ['maybe','probably','perhaps',"honestly man, i really don't know this one"],
 badAnswers: ['LOL NO','no way man','maybe',"forget it",'HELL no','Are you serious?']
};
const randomNumber = function(subject) {
 return Math.floor(Math.random()*subject.length);
}
const types = Object.keys(randomAnswers);
const randomTypeNumber = randomNumber(types);
const randomType = types[randomTypeNumber];
const randomAnswerNumber = randomNumber(randomAnswers[randomType]);
const randomAnswer = randomAnswers[randomType][randomAnswerNumber];
console.log( { randomType, randomAnswer } );

You pick a random key from the object, and then pick a random element from that array by using Object.keys(randomAnswers).length and subsequently that array length for the random numbers.

answered Jun 9, 2019 at 13:37
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.