简体   繁体   English

JavaScript - 数组和子数组

[英]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.我希望问题是两个随机数相加,但我也希望答案也在数组中,在索引 [1] 处,在子数组中,对应于问题 - 与问题具有相同的答案. 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.该代码旨在创建一个 5 个问题的数学测验,该测验使用随机生成的数字创建问题的答案。

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.更好的是,如果有人能以一种我能理解的简单方式做到这一点,因为我是 JavaScript 的新手……所以没有 javascript 黑魔法,谢谢。

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.其中 x 和 y 是随机生成的。 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] .然后,您可以通过questions[i][0] + questions[i][2]获得问题的答案。


PS: I answered your 'question', but you must know that this isn't what StackOverflow is for. PS:我回答了您的“问题”,但您必须知道这不是 StackOverflow 的用途。 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 .有关更多信息,请阅读有关如何提问帮助部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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