Home > Net >  How do I generate a random answer from multiple arrays in an if/else statement?
How do I generate a random answer from multiple arrays in an if/else statement?

Time:04-04

I am currently working on a project that already has provided me with this code on a separate js file:

function getRandomNumber(min, max) { 
    if (_TESTING) {
      _mockRandomNumberCalls.push({min: min, max: max});
      }

     min = Math.ceil(min);
     max = Math.floor(max);
     return Math.floor(Math.random() * (max - min   1)   min);
}

Now, I am supposed to create a function that will call on a random answer from these multiple arrays on the js file I am using:


let positiveAnswers = [ 
    "As I see it, yes",
    "It is certain",
    "It is decidedly so",
    "Yes",
    "Yes, definitely"
];

let negativeAnswers = [
    "My reply is no",
    "My sources say no",
    "There is no way",
    "No",
    "Absolutely not!"
];

let maybeAnswers = [
    "Ask again later",
    "Better to not tell you now",
    "Cannot predict now",
    "Concentrate and ask again",
    "Reply hazy try again"
];

const NO_QUESTION_WARNING = "You need to ask a question!";

So I know I need to use an if else statement for my function in order to get a random answer. In my project though, it says to call on the random answer by just adding 'getRandomNumber' so here is my code:

function chooseRandomAnswer(answerType) {     `

    let answertype = positiveAnswers, negativeAnswers, maybeAnswers;     

    if (answerType = positiveAnswers){       
        getRandomNumber();
        } else { 
          if (answertype = negativeAnswers){
             getRandomNumber();       
          }     
       }

I want to generate a random answer from the arrays that are provided for me, but I keep getting lost and javascript keeps telling me this will not work. I keep trying to find the answer but somewhere along the way I think I messed up my function. I know, I know, it's so evident that I am a beginner and maybe it is easier than I think.

CodePudding user response:

Ok, let's walk through how to solve this problem from a complete beginners' perspective.

With this function,

function chooseRandomAnswer(answerType) {
  // some code here
}

you need to be sure of these two things:

  • what information exactly is being given to the function?
  • what information exactly is being passed out of the function?

Which then begs the question, how do we become sure of these things? Your first step should be to log answerType to the console, like this:

function chooseRandomAnswer(answerType) {
  console.log(answerType);
  console.log(typeof answerType);
}

After this, run your code and check to see what the console says. I don't know exactly what it will show (and I don't think you currently do either), but my guess is that it could show a string of some kind, maybe "positive", "negative", or "maybe". The second console log will help you understand what the "type" is - is it a string, number, boolean, object, or something else?

Once you know, you'll be able to write something like this:

function chooseRandomAnswer(answerType) {
  if (answerType === "positive" /* a guess on my part */) {
    return positiveAnswers[getRandomNumber(0, ...)];
  }
}

I've left the ... in the line so you can explore how to do this on your own. Look up how arrays work for help if you get stuck, this link is good:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

Hope this helps!

CodePudding user response:

I don't exactly know what you are trining to do, but here is a way to generate an random answer

const positiveAnswers = [ "As I see it, yes", "It is certain", "It is decidedly so", "Yes", "Yes, definitely" ];
const negativeAnswers = [ "My reply is no", "My sources say no", "There is no way", "No", "Absolutely not!" ];
const maybeAnswers = [ "Ask again later", "Better to not tell you now", "Cannot predict now", "Concentrate and ask again", "Reply hazy try again" ];
const NO_QUESTION_WARNING = "You need to ask a question!" ;

function chooseRandomAnswer(answerType) 
{  
  var answer = '', nr=0;

  switch (answerType)
  {
    case 'positiveAnswer':
    case 1:
          nr = Math.floor( Math.random() * positiveAnswers.length )
          answer = positiveAnswers[nr]
          break;
      case 'negativeAnswer':
      case -1:
          nr = Math.floor( Math.random() * negativeAnswers.length )
          answer = negativeAnswers[nr]
          break;
      case 'maybeAnswer':
      case 0:
          nr = Math.floor( Math.random() * maybeAnswers.length )
          answer = maybeAnswers[nr]
          break;
      default:
          answer = NO_QUESTION_WARNING
    }
    return answer;
    
  }
  • Related