Why three true values return false!

I’m trying to solve sudoku problem :point_down::point_down::point_down:
https://app.codesignal.com/arcade/intro/level-12/tQgasP8b62JBeirMS
so I decided to cut the problem to three little methods
checkVertically, checkHorizantially, and checkGridSize3

function sudoku(grid) {
    return checkGridSize3(grid)&&checkHorizantially(grid)&&checkVertically(grid);
}

but the final result returns false even these three methods return true seperately

function sudoku(grid) {
    console.log(checkGridSize3(grid));
    console.log(checkVertically(grid));
    console.log(checkHorizantially(grid));
    console.log((checkGridSize3(grid) && checkHorizantially(grid)) && checkVertically(grid));
    return checkGridSize3(grid)&&checkHorizantially(grid)&&checkVertically(grid);
}
 true
 true
 true
 false
 false

I can’t understand what is wrong on the return statment

Can’t help much without seeing the code, but make sure you do not use any global variables that get altered during the execution of those functions and also make sure you do not modify the grid in any of them.

My best guess is that at least one of your functions has side effects and is modifying grid.

1 Like

I tried to put the last function first, but the result was the same
If as you say it should return one true and false for the rest

There’s not much we can say without seeing the code, except that the symptoms you describe make us think that it is probably a matter of function side effects.

it seems that your solution is not complete

This changes the grid.

1 Like

I’m sorry I’m out home, I was trying to do that by my phone
I’ll post my code ASAP

this is my code

function sudoku(grid) {
    console.log(checkVertically(grid)&&checkHorizantially(grid)&&checkGridSize3(grid));
    return checkGridSize3(grid)&&checkHorizantially(grid)&&checkVertically(grid);
}

function checkGridSize3(grid){
    for(let i=0;i<9;i+=3){
        for(let z=0;z<9;z+=3){
            let temp=[];
            for(let row=0;row<3;row++){
                for(let col=0;col<3;col++){
                      temp.push(grid[row][col]);          
                }
            }
        if(!inspectArr(temp)) return false;
        }
    }    
    return true;
}

function checkVertically(grid){
    for( let col=0;col<9;col++){
        let temp=[];
        for(let row=0;row<9;row++){
           temp.push(grid[row][col])         
        }
        if(!inspectArr(temp)) return false;
    }
    return true;
}

function checkHorizantially(grid){
    return grid.every((row) => inspectArr(row));
}

function inspectArr(arr){
    arr=arr.sort((a,b)=> a-b);
    return arr.every((ele, index)=> index+1 ==ele);    
}

sorry for being late

this hint was enough, thanks

I edited the last two functions even though the code worked as expected without changing the checkHorizantially function

function checkHorizantially(grid){
    for ( let i in grid){
        if(!inspectArr(grid[i])) return false;
    }
    return true;
}

function inspectArr(arr){
    for(let i =1;i<10;i++){
        if(!arr.includes(i)) return false;
    }
    return true;
}