Skip to content
Advertisement

Sudoku Validator is not returning correct boolean

I’m writing a sudoku validator and I’ve run into a problem at the very end of it. I’m very new to coding, so please be patient with me. The function sudokuIsValid is supposed to take a puzzle (an array of 9 arrays) and check each column, row, and 3×3 grid to see if it is valid. The function includes1To9 works on its own, getColumn works, getRow works, and getSection works. So from my understanding, it’s just the last function sudokuIsValid. I’m not sure what is wrong with it, probably something minute. I just don’t know anyone else who codes, so I don’t have many people to ask. I understand that this code could likely be written better. I also kindly ask you do not try to introduce me to new operators or things like that.. May you please just explain why this does not work? I don’t understand why sudokuIsValid is not returning the way it should as it is. Thanks for your help. Here’s my code:

JavaScript

Advertisement

Answer

This is due to a small logic error in your code, the final for loop in the sudokuIsValid is wrong specificaly

JavaScript

Your returning true whenever a single row or column, section has all the numbers

change the last for to:

JavaScript

and it worked for me

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement