Javascript 数组索引无法正常工作

Javascript Array Index not working properly

我目前正在尝试制作一个小提示游戏,根据用户选择的类别和难度显示问题。我正在尝试使用尽可能少的代码来做到这一点,以使其最有效。

目前正在发生的事情是,我将类别保存在一个名为 category 的变量中,并将难度值保存在一个名为 difficulty 的变量中。当我尝试访问我的数组时,我使用 category[difficulty].question 因为我已经根据它们的类别命名了我的数组。

但是,它没有索引正确的元素,而是尝试拼写类别的单词。即,如果类别是地理并且难度很简单,我正在尝试使用 category[difficulty].question 访问 geography[0].question。但是我得到的是单词 geography 的第一个字母,因此控制台日志中显示的只是字符串字母 g。

谁能解释为什么会这样,并提出解决方案。

p.s。我知道 switch 语句不再常用,但这是一项作业,我必须使用它们。

//Set up variables

var difficulty;
var geography = [

  {
    question: 'What is the captial of Canada? ',
    answer: 'ottawa',
  },

  {
    question: 'Where are the oil sands located? ',
    answer: 'alberta',
  },

  {
    question: 'What is the capital of Nunavut?',
    answer: 'iqaluit',
  },

  {
    question: 'What is the capital of the Northwest Territories?',
    answer: 'yellowknife',
  },

];

var history = [

  {
    question: 'What year was Canada founded?',
    answer: '1867',
  },

  {
    question: 'What year did we engage in war against the United States?',
    answer: '1812',
  },

  {
    question: 'Difficult history question',
    answer: 'Difficult history answer',
  },

  {
    question: 'Fiendish history question',
    answer: 'Fiendish history answer',
  },

];

var artsAndCulture = [

  {
    question: 'Easy Arts and Culture Question',
    answer: 'Easy Arts and Culture Answer',
  },

  {
    question: 'Medium Arts and Culture Question',
    answer: 'Medium Arts and Culture Answer',
  },

  {
    question: 'Difficult Arts and Culture Question',
    answer: 'Difficult Arts and Culture Answer',
  },

  {
    question: 'Fiendish Arts and Culture Question',
    answer: 'Fiendish Arts and Culture Answer',
  },

];



//Sets up first user prompt and sets the value of category to the value the user entered

var category = prompt("Please select a category:\n\ngeography\nhistory\narts and culture\n");
switch (category) {
  case "geography":
    console.log('You have selected the geography category');
    console.log('The value of category is ' + category);
    break;
  case "history":
    console.log('You have selected the history category');
    console.log('The value of category is ' + category);
    break;
  case "artsAndCulture":
    console.log('You have selected the arts and culture category');
    console.log('The value of category is ' + category);
    break;
  default:
    console.log('default');
}


//Sets up second user prompt and sets the value of difficulty based on the user selection

var input2 = prompt("Please select a difficulty:\n\neasy\nmedium\ndifficult\nfiendish\n");
switch (input2) {
  case "easy":
    console.log('You have selected the easy difficulty');
    difficulty = 0;
    console.log('The value of difficulty is ' + difficulty);
    compareAnswer();
    break;
  case "medium":
    console.log('You have selected the medium difficulty');
    difficulty = 1;
    console.log('The value of difficulty is ' + difficulty);
    compareAnswer();
    break;
  case "difficult":
    console.log('You have selected the difficult difficulty');
    difficulty = 2;
    console.log('The value of difficulty is ' + difficulty);
    compareAnswer();
    break;
    case "fiendish":
    console.log('You have selected the fiendish difficulty');
    difficulty = 3;
    console.log('The value of difficulty is ' + difficulty);
    compareAnswer();
    break;
  default:
    console.log('default');
}


//compares user answer to actual answer
function compareAnswer() {

  var x = prompt(category[difficulty].question);
  console.log(category[difficulty]);
  console.log('You answered ' + x);
  console.log('The correct answer is ' + category[difficulty].answer);
  if (x == category[difficulty].answer) {
    console.log('You answered correctly!');
  } else {
    console.log('You answered incorrectly, the correct answer was ' + category[difficulty].answer);
  }
}

您分配 category 一个字符串值,但将其用作数组引用。

假设您的 javascript 是在全局上下文中编写的:

var x = prompt(window[category][difficulty].question);

其他问题包括 upper/lower 个答案可能存在差异和 \narts and culture\n"); 不匹配 case "artsAndCulture":

我会把类别数组放到一个对象中:

var selection={
  'geography': [{
    question: 'What is the captial of Canada? ',
    answer: 'ottawa'
  },{
    question: 'Where are the oil sands located? ',
    answer: 'alberta'
  },{
    question: 'What is the capital of Nunavut?',
    answer: 'iqaluit'
  },{
    question: 'What is the capital of the Northwest Territories?',
    answer: 'yellowknife'
  }],
'history':[{
    question: 'What year was Canada founded?',
    answer: '1867'
  },
  {
    question: 'What year did we engage in war against the United States?',
    answer: '1812'
  },{
    question: 'Difficult history question',
    answer: 'Difficult history answer'
  },{
    question: 'Fiendish history question',
    answer: 'Fiendish history answer'
  }],
'artsAndCulture': [{
    question: 'Easy Arts and Culture Question',
    answer: 'Easy Arts and Culture Answer'
  },{
    question: 'Medium Arts and Culture Question',
    answer: 'Medium Arts and Culture Answer'
  },{
    question: 'Difficult Arts and Culture Question',
    answer: 'Difficult Arts and Culture Answer'
  },{
    question: 'Fiendish Arts and Culture Question',
    answer: 'Fiendish Arts and Culture Answer'
}]};

然后询问用户使用:

var x = prompt(selection[category][difficulty].question);

尝试将类别统一在一个对象中,smthg like var categories = {geo:[], history:[]};并从此对象中选择类别,例如类别[类别] [难度]。在您的示例类别中,只是一个字符串,从 promt 返回,未链接到数组或对象。