4

I am currently trying to make a little prompt game that displays questions based on the category and difficulty selected by the user. I am trying to make this using the least code that I can so its most efficient.

Currently what is happening, is I am saving the category in a variable called category and the difficulty value in an variable called difficulty. When I try to access my array I use category[difficulty].question as I have named my arrays according to their category.

However, instead of indexing the proper element, it tries to spell the word of the category. i.e. if the category is geography and the difficulty is easy, I am trying to access geography[0].question by using category[difficulty].question. But instead I get the first letter of the word geography so all that displays in the console log is the string letter g.

Can anyone explain why this is happening, and propose a solution.

p.s. I know switch statements aren't commonly used anymore, but this is an assignment and I am required to use them.

//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);
  }
}
j08691
  • 204,283
  • 31
  • 260
  • 272
Kyle Bing
  • 253
  • 2
  • 6
  • 20

3 Answers3

3

You assign category a string value, but use it as array reference.

Assuming your javascript is written in global context:

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

Other issues include possible discrepancies in upper/lower case of answers and \narts and culture\n"); not matching case "artsAndCulture":.

Igor
  • 15,833
  • 1
  • 27
  • 32
  • beat me to it. As an explanation: `window[string value]` will convert the string value into a variable; if it exists. – William_Wilson Oct 30 '15 at 18:51
  • Thanks very much i was unaware that using window in front of my array indexing would covert it into a variable. I chose the other answer listed as best answer but still gave yours an upvote as its very useful, i just think the other answer serves me best in this particular instance – Kyle Bing Oct 30 '15 at 19:06
  • @KyleBing - it is not "window in front", but a different way to access properties of javascript objects using their names (as strings) in []. `window` is used here because variables defined in global context become properties of global `window` object. Happy coding. – Igor Oct 30 '15 at 19:10
  • @Igor thank you for the clarification, i am stilla novice when it comes to js but am trying my best to improve, happy coding to you as well – Kyle Bing Oct 30 '15 at 19:18
3

I would but the category arrays into an object:

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'
}]};

and then to question the user use:

var x = prompt(selection[category][difficulty].question);
depperm
  • 10,606
  • 4
  • 43
  • 67
  • Note that this answer also fixes the syntax error of the OP's trailing commas before the closing square brace. IOW, there should be no comma after the last element in the array. – Jared Oct 30 '15 at 18:54
  • @Jared Trailing commas are valid in ES5 objects and arrays, but not in JSON data string though. – Teemu Oct 30 '15 at 18:58
1

Try to unite categories in one object, smthg like var categories = {geo:[], history:[]}; and pick categories from this object, to example categories[category][difficulty]. in your example category just a string, returned from promt, not linked to array or object.

Dmitry Lobov
  • 313
  • 1
  • 10