【发布时间】:2015-10-30 18:41:58
【问题描述】:
我目前正在尝试制作一个小提示游戏,它会根据用户选择的类别和难度显示问题。我正在尝试使用最少的代码来做到这一点,因此它最有效。
目前正在发生的事情是,我将类别保存在一个名为类别的变量中,并将难度值保存在一个名为难度的变量中。当我尝试访问我的数组时,我使用category[difficulty].question,因为我根据数组的类别命名了我的数组。
但是,它不是索引正确的元素,而是尝试拼写类别的单词。即如果类别是地理并且难度很简单,我正在尝试使用category[difficulty].question 访问geography[0].question。但相反,我得到了 geography 的第一个字母,所以控制台日志中显示的只是字符串字母 g。
谁能解释为什么会发生这种情况,并提出解决方案。
附言我知道 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);
}
}
【问题讨论】:
标签: javascript arrays object switch-statement