-1

I have this javascript code that is not working. Althought the first conditional(if conditional) is working, the else conditional is not working. Sorry for my bad english.

function choice1()
    {
        if(pic_location<9)
        {
            if(document.getElementById("choice_1").value==character[numbers[pic_location]])
            {
                window.alert("Guess Correct!");
                score = score + 1;
                document.getElementById("score").innerHTML = score;
                pic_location=pic_location + 1;
                document.getElementById("pic").setAttribute("src", "pictures/" + character[numbers[pic_location]] + ".jpg");
            }
            else
            {
                window.alert("Wrong Answer!");
                pic_location=pic_location + 1;
                document.getElementById("pic").setAttribute("src", "pictures/" + character[numbers[pic_location]] + ".jpg");
            }
            set_choices();
            document.getElementById("qn").innerHTML= pic_location + 1;
        }
        else
        {
            if(pic_location==10)
            {
                document.getElementById("choice_1").value==character[numbers[pic_location]];
                window.alert("Guess Correct");
                score = score +1;
                window.location="game3.php";
            }
            else
            {
                window.alert("Wrong Guess!");
                window.location="game3.php";
            }
        }
        
    }

The if function is working fine when i run it but when I get to the last photo location which is 10, it always guesses wrong even though the answer is right.

Note: my function is the same for the four choices. They all have the same problems.

  • What is `character`? – Dai Feb 17 '23 at 02:02
  • Many of your variables, like `pic_location`, `score`, `character`, and others, look like mutable global variables, which is a bad-practice. Is there a reason you're not using function-parameters instead? – Dai Feb 17 '23 at 02:03
  • `window.location="game3.php";` <-- Setting `window.location` will trigger an immediate redirect and defeats the point of mutating variables beforehand (like `score = score + 1`, also consider using `score += 1` or `score++` instead to be more succinct. – Dai Feb 17 '23 at 02:04
  • Use linters like [ESLint](//eslint.org/play) or [JSHint](//jshint.com) to find problems with your code immediately. Relevant linter warning: _“Expected an assignment or function call and instead saw an expression.”_. – Sebastian Simon Feb 17 '23 at 02:24
  • Hello, thank you for your suggestions. I applied these to my current code and it worked. Thank you! – chusaurous Feb 17 '23 at 07:41

1 Answers1

0

You are using comparison operator == instead of assignment =

 if(pic_location==10)
        {
            document.getElementById("choice_1").value==character[numbers[pic_location]];
            window.alert("Guess Correct");
            score = score +1;
            window.location="game3.php";
        }
Talmacel Marian Silviu
  • 1,596
  • 2
  • 6
  • 14