简体   繁体   中英

JavaScript - Arrays & Sub-Arrays

function print(message) {
  document.write(message);
}

function randomNumber (range) {
  return Math.floor(Math.random() * range + 1);
}

var questions = [
  ['What is ' randomNumber(10) + randomNumber(10) + '?', '''displayanswerhere''']
  ['What is 4 + 2?', randomNumber(10)],
  ['What is 8 + 8?', randomNumber(10)],
  ['What is 1 + 7?', randomNumber(10)],
  ['What is 8 + 6?', randomNumber(10)],
];

function quiz (questions) {
  var score;
  var wrongAnswers = correctAnswers = '<ol>';
  for (i = 0; i < questions.length; i += 1) {
    var userAnswer = parseInt(prompt(questions[i][0]));
    if (userAnswer === questions[i][1]) {
      score += 1;
      correctAnswers += '<li>' + questions[i][0] + '</li>';
    } else {
        wrongAnswers += '<li>' + questions[i][0] + '</li>';
    }
  }
  function printScores (wrongAnswers, correctAnswers, score) {
    print('<p> You got ' + score + ' questions right.</p>');
    print('<h2> You got these questions correct:</h2>');
    print(correctAnswers + '</ol>');
    print('<h2> You got these questions incorrect:</h2>');
    print(wrongAnswers + '</ol>');
  }
  printScores(wrongAnswers, correctAnswers, score);
}
quiz(questions);

In the two dimensional array 'questions', I want the sub-arrays to consist of a question and an answer. I want the question to be two random numbers added together but I also want the answer which is also in the array, at the index [1], in the sub-array, to correspond to the question - have the same answer as the question. I'm not sure how to do this because I have used a function to generate the random numbers and each time I call it a new random number is returned.

The code is intended to create aa 5 question math quiz that creates answers with questions with randomly generated numbers.

I would appreciate if someone could solve my problem. What would be even better is if somebody could do it in a simple way that I could understand because I'm new to JavaScript... so no javascript black magic, thanks.

If I understand you correctly, you want to do something like this:

var questions = [
    ['What is ' + x + ' + ' + y + '?', x + y],
    ['What is ' + x + ' + ' + y + '?', x + y],
    ['What is ' + x + ' + ' + y + '?', x + y],
];

Where x and y are randomly generated on the fly. So your real question is how can I access the value of other cells of an array while creating the array to which the answer is "it's impossible", especially if you store the values as part of a question in a string. Instead, try something like this:

var questions = [
  [randomNumber(10), '+', randomNumber(10)],
  [randomNumber(10), '+', randomNumber(10)],
  [randomNumber(10), '+', randomNumber(10)],
];

Then modify your functions so that they print a question made with the values. Something like
prompt('What is ' + questions[i][0] + ' ' + questions[i][1] + ' ' + questions[i][2] + '?')

And then, you can get the answer to your question with questions[i][0] + questions[i][2] .


PS: I answered your 'question', but you must know that this isn't what StackOverflow is for. Questions here have to be real questions, not problems to solve, so you should always make sure to write in in a way that allows people with the same problem to find it. For more info, read the help section about how to ask .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM