Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Array Index not working properly

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);
  }
}
like image 331
Kyle Bing Avatar asked Mar 14 '23 07:03

Kyle Bing


1 Answers

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);
like image 78
depperm Avatar answered Mar 23 '23 13:03

depperm