Bailey Moir
Bailey Moir

Reputation: 3

JavaScript if else problems

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.

Upvotes: -1

Views: 112

Answers (3)

user8660130
user8660130

Reputation:

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
}

Upvotes: 0

Paul
Paul

Reputation: 36319

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

Upvotes: 2

Elliott Frisch
Elliott Frisch

Reputation: 201447

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)";
}

Upvotes: 2

Related Questions