-1

everyone! I have been having problems with my code. I think I know what is wrong but I can't seem to fix it, no matter how much I tried so I decided to take it up with the community. I think it is because of the second if statements contradict the one before it. Here, take a look.

        if (Character.style.backgroundImage === "url(../images/animations/moveRightAnimation/1.png)") {

            Character.style.backgroundImage = "url(../images/animations/moveRightAnimation/2.png)";

        } else (Character.style.backgroundImage != "url(../images/animations/moveRightAnimation/1.png)") {

            Character.style.backgroundImage = "url(../images/animations/moveRightAnimation/1.png)";

Hopefully, you see what I am talking about and know what the answer is. As I said in my last post I am not a great coder, so don't judge me too hard, XD.

3
  • 2
    Please update your question to explain what the "problems" you are having are. Commented Sep 24, 2017 at 3:14
  • 4
    You don't need to put condition in else. Commented Sep 24, 2017 at 3:15
  • What would you like to achieve, your desired result if you will Commented Sep 24, 2017 at 3:16

3 Answers 3

2

There is no reason to repeat the conditional check negated with an else, that is only needed for an else if. Change

} else (Character.style.backgroundImage != "url(../images/animations/moveRightAnimation/1.png)") {
    Character.style.backgroundImage = "url(../images/animations/moveRightAnimation/1.png)";
}

to something like

} else {
    Character.style.backgroundImage = 
            "url(../images/animations/moveRightAnimation/1.png)";
}
Sign up to request clarification or add additional context in comments.

Comments

2

Else doesn't take any parameters it's literally what runs in the case the if evaluates false.

If you want multiple exclusive if blocks, you need to use 'else if' where you currently have else

Comments

0

Syntax for if condition :

if ( condition ){
// code 
}
else {
// code
}

Note : After else there cant't be any condition . Or you must use if-else.(Only if you want to use multiple conditions.And you seem to check only one,so its not needed.)

Syntax for if condition :

if ( condition ){
// code 
}
else if ( condition ){
// code
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.