Object w/ if statement: Trying to accomplish a specific task

I’m sending values to an object as buttons are clicked. I wrote a conditional under the object so that if, say, the first and second keys have the same value, then an answer is consoled out. For example:

let obj = {
1: 'a',
2: 'a',
3: 'b',
4: 
};

if(obj[1] === 'a' && obj[2] === 'a') {
     console.log('...')
};

The issue here is that the if statement does not read the object when values are send to it. On the other hand, if an object already has keys with values, then the if statement reads it.

Is that the case, because the code is not being read when I send the values? So, then, I would have to make that be the case?

You currently have invalid syntax here, so it will error out and not log anything to the console.

But if you provide a valid object like:

let obj = {
  1: 'a',
  2: 'a',
  3: 'b',
  4: 'c'
};

then the following will display “found match”

if(obj[1] === 'a' && obj[2] === 'a') {
     console.log('found match');
};

I am not really sure what you are saying here. You may need to show us the full code and explain how to replicate the issue your are talking about.

you can try bracket notation with quotes

> obj["1"]

This is the code. I’m using buttons (HTML) to send player1 and player 2 to become values of 1 , 2, 3, 4.

let player = '';
let firstChoice = ['player11'];
console.log(firstChoice);
let firstChoice1 = 'player11';

let quadrants = {
    1: '',
    2: '',
    3: '',
    4: ''
};
console.log(quadrants);

if(quadrants[1] === 'player1' && quadrants[3] === 'player1') {
    console.log('W');
};


let lever = 'off';
function quadrantOne() {
    let captureFirstChoice;
    captureFirstChoice = firstChoice.pop();

    console.log(firstChoice);
    if(firstChoice1 === captureFirstChoice) {
        quadrants[1] = 'player1';
        lever = 'on'; 
    };    
        if(quadrants[1] !== 'player1') {
            player = 'player2';
            if(player === 'player2' && lever === 'on') {
                quadrants[1] = 'player2';
                lever = 'off'
            }
            if(lever === 'off' && quadrants[1] !== 'player2') {
                player = 'player1';
                if(player === 'player1') {
                    quadrants[1] = 'player1';
                    lever = 'on';
                };
            };

        };
};
function quadrantTwo() {
    let captureFirstChoice;
    captureFirstChoice = firstChoice.pop();
    console.log(firstChoice);
    if(firstChoice1 === captureFirstChoice) {
        quadrants[2] = 'player1';
        lever = 'on'; 
    };    
        if(quadrants[2] !== 'player1') {
            
            player = 'player2';
            if(player === 'player2' && lever === 'on') {
                quadrants[2] = 'player2';
                lever = 'off'
            }
            if(lever === 'off' && quadrants[2] !== 'player2' ) {
                player = 'player1';
                if(player === 'player1') {
                    quadrants[2] = 'player1';
                    lever = 'on';
                };
            };

        }; 
};

It’s also on Github:

I tried it, but it did not work.

Thanks for posting the GitHub repo. That gives a better idea of what you are attempting to do.

Walk us through clicking various buttons and what you expect to happen vs. what actually happens after each click. Be as detailed as possible so we understand what you are trying to accomplish.

Your code seems overly complex and you should notice there is a lot of repetitive code. Whenever you see repeated code, there is an opportunity to create function with the repeated parts. It will make your code more readable to others and simply your app’s logic.

1 Like

Unless I’m not understanding the question, your top-level if statement (the one after the object) runs one time at the start. When it runs the conditions are not met and as it never runs again (until the code is re-executed) they are never met.

let someCondition = false;

if (someCondition) {
  console.log('I will never run');
}

function changeCondition() {
  someCondition = true;
}

changeCondition();

if (someCondition) {
  console.log('I will run');
}

I would suggest using the debugger and looking at the execution flow.

JavaScript Tutor - Visualize JavaScript code execution to learn JavaScript online

1 Like

You should add console.log at the end of function to check the quadrant status

function quadrantOne() {
    let captureFirstChoice;
    captureFirstChoice = firstChoice.pop();

    console.log(firstChoice);
    if(firstChoice1 === captureFirstChoice) {
        quadrants[1] = 'player1';
        lever = 'on'; 
    };    
        if(quadrants[1] !== 'player1') {
            player = 'player2';
            if(player === 'player2' && lever === 'on') {
                quadrants[1] = 'player2';
                lever = 'off'
            }
            if(lever === 'off' && quadrants[1] !== 'player2') {
                player = 'player1';
                if(player === 'player1') {
                    quadrants[1] = 'player1';
                    lever = 'on';
                };
            };

        };
console.log(quadrants)
};

I was able to solve the problem: it had to do with scope. I created a function called validation that contains the if statements that will check if player1 or player2 won (tic-tac-toe: three marks in a roll either horizontally, vertically or diagonally). Then I placed the validation function inside and at the botton of quadrantsAll function that contains the logic that switches from player1 to player2 and vice versa, so validation can know the which player chose which quadrant.

I’m posting the full code here; it is also on my Github.

let player = 'player1';
let firstChoice = ['player11'];
let firstChoice1 = 'player11';
let captureFirstChoice;

let quadrants = {
    1: '',
    2: '',
    3: '',
    4: '',
    5: '',
    6: '',
    7: '',
    8: '',
    9: ''
};
function validation(val) {
    //console.log(val);
    if((quadrants[1] === 'player1' && quadrants[2] === 'player1' && quadrants[3] === 'player1') || (quadrants[4] === 'player1' && quadrants[5] === 'player1' && quadrants[6] === 'player1') || (quadrants[7] === 'player1' && quadrants[8] === 'player1' && quadrants[9] === 'player1') //horizontal
    || (quadrants[1] === 'player1' && quadrants[4] === 'player1' && quadrants[7] === 'player1') || (quadrants[2] === 'player1' && quadrants[5] === 'player1' && quadrants[8] === 'player1') || (quadrants[3] === 'player1' && quadrants[6] === 'player1' && quadrants[9] === 'player1') //vertical
    || (quadrants[1] === 'player1' && quadrants[5] === 'player1' && quadrants[9] === 'player1') || (quadrants[3] === 'player1' && quadrants[5] === 'player1' && quadrants[7] === 'player1')) { //diagonal
        console.log('player 1 wins');
    };
    if((quadrants[1] === 'player2' && quadrants[2] === 'player2' && quadrants[3] === 'player2') || (quadrants[4] === 'player2' && quadrants[5] === 'player2' && quadrants[6] === 'player2') || (quadrants[7] === 'player2' && quadrants[8] === 'player2' && quadrants[9] === 'player2') //horizontal
    || (quadrants[1] === 'player2' && quadrants[4] === 'player2' && quadrants[7] === 'player2') || (quadrants[2] === 'player2' && quadrants[5] === 'player2' && quadrants[8] === 'player2') || (quadrants[3] === 'player2' && quadrants[6] === 'player2' && quadrants[9] === 'player2') //vertical
    || (quadrants[1] === 'player2' && quadrants[5] === 'player2' && quadrants[9] === 'player2') || (quadrants[3] === 'player2' && quadrants[5] === 'player2' && quadrants[7] === 'player2')) {
        console.log('player 2 wins');
    };
};
console.log(quadrants);

let lever = 'off';
function quadrantsAll(val) {
    if(firstChoice1 === captureFirstChoice) {
        quadrants[val] = player;
        lever = 'on'; 
    };    
        if(quadrants[val] !== 'player1') {
            player = 'player2';
            if(player === 'player2' && lever === 'on') {
                quadrants[val] = 'player2';
                lever = 'off'
            }
            if(lever === 'off' && quadrants[val] !== 'player2') {
                player = 'player1';
                if(player === 'player1') {
                    quadrants[val] = 'player1';
                    lever = 'on';                    
                };  
            };  
        };
        validation();
};
function quadrantOne() {
    captureFirstChoice = firstChoice.pop();
    quadrantsAll(1);
};
function quadrantTwo() {
    captureFirstChoice = firstChoice.pop();
    quadrantsAll(2);
};
function quadrantThree() {
    captureFirstChoice = firstChoice.pop();
    quadrantsAll(3);
};
function quadrantFour() {
    captureFirstChoice = firstChoice.pop();
    quadrantsAll(4);
};
function quadrantFive() {
    captureFirstChoice = firstChoice.pop();
    quadrantsAll(5);
};
function quadrantSix() {
    captureFirstChoice = firstChoice.pop();
    quadrantsAll(6);
};
function quadrantSeven() {
    captureFirstChoice = firstChoice.pop();
    quadrantsAll(7);
};
function quadrantEight() {
    captureFirstChoice = firstChoice.pop();
    quadrantsAll(8);
};
function quadrantNine() {
    captureFirstChoice = firstChoice.pop();
    quadrantsAll(9);
};

If anyone has any tips on anything that I can do to improve, please, let me know.

Thanks, @lasjorg ! I do have to spend some time studying these topics.