Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9241593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:23:50+00:00 2026-06-18T08:23:50+00:00

Okay, so this is my problem. I’m kind of new to javascript so plz

  • 0

Okay, so this is my problem. I’m kind of new to javascript so plz bear with me. I haven’t found this issue somewhere else, and actually I think it’s my logic that doesn’t corespond to my willingness 😉

I’m setting up a memory game with php and javascript. My problem is that when I first execute the clickCard-function it sets the variable firstClick to True with it’s innerHTML-value and executes secondClick etc so long so fine, but if the guess is wrong only secondClick turns red. This because I believe that the first argument is static (and therfore saves the value of the color for firstClick), and I can’t come up with a solution on how to solve this. I’ve tried diffrent things like creating conditions like if else(firstClick != 0 && firstClick != secondClick) {code} etc.

Anyone that can help me?

This is my code:

var firstClick = new Boolean();
firstClick.toString();

var secondClick = new Boolean();
secondClick.toString();
function clickCard(elem){               

    if(firstClick == 0){
        firstClick = document.getElementById(elem.id).innerHTML;
                    elem.style.backgroundColor = "green";   
        //alert(firstClick);
        }

    else{

        secondClick = document.getElementById(elem.id).innerHTML;
        //alert(secondClick);
        elem.style.backgroundColor = "green";


            if(firstClick == secondClick){
                alert(firstClick +" equals "+ secondClick);
                firstClick = new Boolean();
                secondClick = new Boolean();
            }

            else {
                alert(firstClick + " equals not " + secondClick);
                firstClick = new Boolean();
                secondClick = new Boolean();
                document.getElementById(elem.id).style.backgroundColor="red";
            }   
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:23:51+00:00Added an answer on June 18, 2026 at 8:23 am

    You have several problems in your code. Take a look at my changes and try to understand them, and it should work:

    1. initialize booleans with var bool = true/false;
    2. don’t mix the boolean with dom-elements
    3. save your tiles in separate variables
    4. actually set the color of the first tile to “red”

    code:

    var firstClick = true;
    var piece1;
    var piece2;
    
    function clickCard(elem){               
    
        if(firstClick){
            piece1 = document.getElementById(elem.id);
            piece1.style.backgroundColor = "green";
            firstClick = false;  
        } else {
    
            piece2 = document.getElementById(elem.id);
            piece2.style.backgroundColor = "green";
    
            if(piece1.innerHTML == piece2.innerHTML){
                alert(firstClick +" equals "+ secondClick);
            } else {
                alert(firstClick + " equals not " + secondClick);
                piece1.style.backgroundColor="red";
                piece2.style.backgroundColor="red";
            }   
            firstClick = true;
        }
    }
    

    Now the advanced practice would be to put this into a closure to protect your “internal” values firstClick and piece1/2, but that’s another story;)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay guys,I need to solve this problem using minimum if else condition.Let me explain
Okay so I am having this problem. I write up a script to be
Okay, it took me a little while to narrow down this problem, but it
Okay guys, basically the problem I'm having is this. I've been assigned to write
Okay this is my 4th question today on Scheme, still pretty new to Scheme,
Just come across with this problem: List<DataNode> a1 = new ArrayList<DataNode>(); List<Tree> b1 =
Okay i'm a little bit stuck on how to solve this problem. When a
Okay, so I have this problem with my Fact Table. I need it to
Having this problem. Seems to deploy okay then the problem reappears... Can't figure out
Okay, this problem is indeed a challenge! Background I am working on an arithmetic-based

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.