-1

I'm new to this site and currently doing a computer programming class up in ontario, canada. I have a computer programming class and currently making an interactive tic-tac-toe game. I use if statements in a function in order to verify in theres a winner. I have an If and an else if. However only the first if works and does its code. When i try to do the conditions for do into the else if it doesn't works. However, if i swap my else if and if so that both conditions are swapped. again only first first works and second will never work. so to me it sounds like my conditions are good. i dont know if this makes sense lol

function verifie_gagnant()
{
if (document.getElementById("centregauche").firstChild.classList.contains("markX") && 
   document.getElementById("centrecentre").firstChild.classList.contains("markX") &&
   document.getElementById("centredroite").firstChild.classList.contains("markX") || 
   document.getElementById("centregauche").firstChild.classList.contains("markO") && 
   document.getElementById("centrecentre").firstChild.classList.contains("markO") &&
   document.getElementById("centredroite").firstChild.classList.contains("markO"))
{
  document.getElementById("winner").innerHTML= tours + " a ete vaincu!!";
} 
else if (document.getElementById("hautgauche").firstChild.classList.contains("markX") && 
    document.getElementById("hautcentre").firstChild.classList.contains("markX") &&
    document.getElementById("hautdroite").firstChild.classList.contains("markX") || 
    document.getElementById("hautgauche").firstChild.classList.contains("markO") && 
    document.getElementById("hautcentre").firstChild.classList.contains("markO") &&
    document.getElementById("hautdroite").firstChild.classList.contains("markO"))
{
  document.getElementById("winner").innerHTML= tours + " a ete vaincu!!";
}
}
3
  • 3
    To test either of 2 sets you need parentheses: if ((a && b) || (d && e)) Commented Mar 2, 2018 at 15:35
  • Both branches have the same effect o.O Commented Mar 2, 2018 at 15:36
  • thanks for the help, parentheses didn't work. its a tictac toe game wich is played within a table of 3 x 3 . the first if tests horizontaly if 3 X's(markX) are line up , OR if 3 O's are lined up and then shows who won. second is the same but tests a different row Commented Mar 2, 2018 at 19:16

2 Answers 2

1

By hearing to your problem it looks like in all the cases both the condition are true. Hence which ever condition is first it executes that first skipping the else part. You can test this removing the else and keeping both in separate if condition. You will notice it navigate inside both if condition. You need to change you IF condition. See below to know more on If condition

IF is a Conditional Statements. ... Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.

Sign up to request clarification or add additional context in comments.

Comments

0

You have an || between && conditions which is evaluating to true in both if and else if.

So even if you swap conditions it always execute first one.

Try putting ( condition1 && condition2) || (condition3 && condition4) in both if and elseif conditions

Example fix. (You need to see where actually you need to group conditions)

function verifie_gagnant()
{
if ((document.getElementById("centregauche").firstChild.classList.contains("markX") && 
   document.getElementById("centrecentre").firstChild.classList.contains("markX") &&
   document.getElementById("centredroite").firstChild.classList.contains("markX")) || 
   (document.getElementById("centregauche").firstChild.classList.contains("markO") && 
   document.getElementById("centrecentre").firstChild.classList.contains("markO") &&
   document.getElementById("centredroite").firstChild.classList.contains("markO")))
{
  document.getElementById("winner").innerHTML= tours + " a ete vaincu!!";
} 
else if ((document.getElementById("hautgauche").firstChild.classList.contains("markX") && 
    document.getElementById("hautcentre").firstChild.classList.contains("markX") &&
    document.getElementById("hautdroite").firstChild.classList.contains("markX")) || 
    document.getElementById("hautgauche").firstChild.classList.contains("markO") && 
    document.getElementById("hautcentre").firstChild.classList.contains("markO") &&
    document.getElementById("hautdroite").firstChild.classList.contains("markO")))
{
  document.getElementById("winner").innerHTML= tours + " a ete vaincu!!";
}
}

This is the first thing you can fix (syntax) after this you need to actually see what is the actual data returned after the single condition is evaluated. Maybe both have same data.

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.