-2

I have a JavaScript programme that randomly selects an image of an object from the database and display that image to the user. The image is saved with the object name (i.e. apple.gif) I am using the following code in order to check whether the user input response is the correct answer to the test or not:

However the code does not do anything. Could anyone let me know what the problem is please?

james
  • 1
  • 3
  • so `console.log(userInput, stringToCheckAgainst)` and check the function is being invoked and what the values actually are. (Avoid `document.write` also and update an elements `innerHTML`) – Alex K. Jul 15 '15 at 11:52
  • you've defined a function, but have not called it - so, it wont do anything – Jaromanda X Jul 15 '15 at 11:52
  • you're also using `document.write` probably after the document has finished loading – Jaromanda X Jul 15 '15 at 11:53
  • i am using '' – james Jul 15 '15 at 11:56
  • does this array has data `random_image_array` and what is this `num` variable? both has value? can you check this? – SK. Jul 15 '15 at 11:59
  • possible duplicate of [How to check if a string array contains one string in JavaScript?](http://stackoverflow.com/questions/12623272/how-to-check-if-a-string-array-contains-one-string-in-javascript) – SK. Jul 15 '15 at 11:59
  • var random_images_array = ['apple.gif, .....]; var num = 0; – james Jul 15 '15 at 12:02

2 Answers2

0

Look at your JavaScript console:

Uncaught ReferenceError: random_image_array is not defined

You made a typo. When you defined the variable you said images (with an s).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Is there any way that I can make the user response not case sensitive, I mean if the user type in with lower case the system convert it to upper case automatically, because the name of the objects are all uppercase. And JavaScript is case sensitive. – james Jul 15 '15 at 17:37
-1
function checkUserInput() {
   var userInput = document.getElementById("textInput").value;
   var stringToCheckAgainst = random_image_array[num].split('.');
   //this splits the item at the array index into an array, like so. If the item is "apple.gif", the array reads ["apple", "gif"]
   if (userInput == stringToCheckAgainst[0]) {
      //user has inputted the correct string
      document.write("Correct!");
   } else {
      //user has inputted an incorrect string
      document.write("Incorrect response!");
   }
}

The above is a function. In order to use the function, you must call it.

Lansana Camara
  • 9,497
  • 11
  • 47
  • 87