Javascript数组索引无法正常工作

时间:2015-10-30 18:41:58

标签: javascript arrays object switch-statement

我目前正在尝试制作一个小型提示游戏,根据用户选择的类别和难度显示问题。我试图用尽可能少的代码来实现这一点,因此它最有效。

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

但是,它不是索引正确的元素,而是尝试拼写该类别的单词。即如果该类别是地理位置且难度很大,我尝试使用geography[0].question访问category[difficulty].question。但相反,我得到了地理一词的第一个字母,所以在控制台日志中显示的是字符串字母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);
  }
}

3 个答案:

答案 0 :(得分:3)

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

假设您的javascript是在全球范围内编写的:

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

其他问题包括答案的大写/小写可能存在差异,而\narts and culture\n");case "artsAndCulture":不匹配。

答案 1 :(得分:2)

我希望类别数组成为一个对象:

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);

答案 2 :(得分:1)

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