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.
-
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?NineBerry– NineBerry2019年06月09日 13:35:07 +00:00Commented Jun 9, 2019 at 13:35
-
Actually, it would be nice if it wouldn't pick the same array 2 times after eachother.Arthur Robaeys– Arthur Robaeys2019年06月09日 13:38:24 +00:00Commented Jun 9, 2019 at 13:38
1 Answer 1
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.