Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to make a code that will pick a random image from a list and ask the user to try and identify it. So far I can generate the image but have run into trouble trying to compare the string with the array element.

var imageArray=new Array()
myImages[1]="image1.jpg"
myImages[2]="image2.jpg"
myImages[3]="image3.jpg"

var randNum=Math.floor(Math.random()*imageArray.length);

function pickImg()
{
if {randNum==0)
{
randNum=1;
}
document.write('<img src="'+imageArray[randNum]+'" border=0>');
}

pickImg()

function checkAns()
{
var str = imageArray[randNum];
var n = str.search('textBox');
if(n = -1)
{
alert{"Wrong Answer")
}
Else
{
alert("Right Answer")
}
}

I am at a loss when it comes to the second function and comparing the two strings. Thank you for any help.

share|improve this question
it should be if(n == -1) – janith Dec 5 '12 at 6:24
...also should be else, not Else – Alexander Pavlov Dec 5 '12 at 6:25
@AlexanderPavlov damn, there are too many... – xiaoyi Dec 5 '12 at 6:26
4  
Lots of mistakes going on there... I'd suggest you start learning JS from scratch the right way. You can begin here developer.mozilla.org/en-US/docs/JavaScript – elclanrs Dec 5 '12 at 6:26
Maybe i should revert the editing, it's kinda answering his question. – xiaoyi Dec 5 '12 at 6:29

closed as not a real question by casperOne Dec 5 '12 at 13:11

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, see the FAQ.

2 Answers

Assuming your input has ID textBox, you should use:

var check = str.indexOf(document.getElementById('textBox').value);
if(check == -1)
{
...
  • search expects a regular expression object as an argument, not a string
  • The = operator assigns values, whereas the == operator compares values
  • JS is case sensitive: you're looking for else, not Else
  • Indentation is very important for maintainable code.
share|improve this answer

There is a mistake in your code

if(n = -1)//Assigns -1 as the value of 'n'(Used assignment operator here)
{
   alert{"Wrong Answer")
}
Else
{
   alert("Right Answer")
}

Try to change the code to

if(n == -1)//Checking whether the value of 'n' is -1 or not(comparison)
{
    alert("Wrong Answer")
}
else
{
   alert("Right Answer")
}
share|improve this answer
Please take the time to explain assignment and comparison. =) – David Thomas Dec 5 '12 at 6:26
Thankyou David:) – Anand Dec 5 '12 at 6:30
there is one more syntax error in your code. alert{ – xiaoyi Dec 5 '12 at 6:33
Thanks for all the responses! I am trying to match user input with part of an array element to make a simple memorization game. The page will then display if the entry was correct or incorrect. – Alex Dec 5 '12 at 6:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.