I tried everything on the online editor and I still get an error. But when I do it on VSCode on my machine it works fine. I am very confused and can't submit the code without finding the error. I have no clue where to look anymore. I would love to have another pair of eyes take a look at this for me. I really appreciate it.
//puzzle
let puzzle = [
[8, 9, 5, 7, 4, 2, 1, 3, 6],
[2, 7, 1, 9, 6, 3, 4, 8, 5],
[4, 6, 3, 5, 8, 1, 7, 9, 2],
[9, 3, 4, 6, 1, 7, 2, 5, 8],
[5, 1, 7, 2, 3, 8, 9, 6, 4],
[6, 8, 2, 4, 5, 9, 3, 7, 1],
[1, 5, 9, 8, 7, 4, 6, 2, 3],
[7, 4, 6, 3, 2, 5, 8, 1, 9],
[3, 2, 8, 1, 9, 6, 5, 4, 7],
];
//puzzle 2
let puzzleTwo = [
[8, 9, 5, 7, 4, 2, 1, 3, 6],
[8, 7, 1, 9, 6, 3, 4, 8, 5],
[4, 6, 3, 5, 8, 1, 7, 9, 2],
[9, 3, 4, 6, 1, 7, 2, 5, 8],
[5, 1, 7, 2, 3, 8, 9, 6, 4],
[6, 8, 2, 4, 5, 9, 3, 7, 1],
[1, 5, 9, 8, 7, 4, 6, 2, 3],
[7, 4, 6, 3, 2, 5, 8, 1, 9],
[3, 2, 8, 1, 9, 6, 5, 4, 7],
];
//DO NOT EDIT ABOVE
function getRow(puzzle, row) {
let array = [];
for (let i = 0; i < puzzle.length; i++) {
if (i === row)
for (let j = 0; j < puzzle[i].length; j++) array.push(puzzle[i][j]);
}
return array;
}
function getColumn(puzzle, col) {
let array = [];
for (let i = 0; i < puzzle.length; i++) {
for (let j = 0; j < puzzle[i].length; j++)
if (j === col) array.push(puzzle[i][col]);
}
return array;
}
function getSection(puzzle, x, y) {
let array = [];
let xIndex = 0;
let yIndex = 0;
if (x === 0) yIndex = 0;
else if (x === 1) yIndex = 3;
else yIndex = 6;
if (y === 0) xIndex = 0;
else if (y === 1) xIndex = 3;
else xIndex = 6;
for (let i = xIndex; i < xIndex + 3; i++)
for (let j = yIndex; j < yIndex + 3; j++) array.push(puzzle[i][j]);
return array;
}
function includes1To9(arr) {
let prev = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i] === prev) return false;
prev = arr[i];
}
return true;
}
function sudokuIsValid(puzzle) {
for (let x = 0; x < 3; x++)
for (let y = 0; y < 3; y++)
if (includes1To9(getSection(puzzle, x, y)) === false) return false;
for (let i = 0; i < puzzle.length; i++) {
if (includes1To9(getRow(puzzle, i)) === false) return false;
if (includes1To9(getColumn(puzzle, i)) === false) return false;
}
return true;
}
console.log(sudokuIsValid(puzzle)); //Returns true
console.log(sudokuIsValid(puzzleTwo)); //Returns false
This is the error message that I get. The prompt is asking me to return false whenever the sudoku is invalid. As you can see it does return false for invalid puzzles but not all invalid puzzles.
FAIL ./index.test.js
sudoku sudokuIsValid
✓ returns false for an invalid puzzle (2 ms)
✕ returns false for other invalid puzzles (2 ms)
● sudoku sudokuIsValid › returns false for other invalid puzzles
expect(received).toBe(expected) // Object.is equality
Expected: false
Received: true
21 | ];
22 | let result = sudoku.sudokuIsValid(puzzle)
> 23 | expect(result).toBe(false);
| ^
24 | });
25 |
26 | });
at Object.<anonymous> (index.test.js:23:20)
Your includes1To9()
only checks if there are adjacent elements the same or not:
function includes1To9(arr) {
let prev = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i] === prev) return false;
prev = arr[i];
}
return true;
}
console.log(includes1To9([1, 2, 1, 4, 5, 6, 7, 8, 9]));
console.log(includes1To9([1, 2, 2, 4, 5, 6, 7, 8, 9]));
console.log(includes1To9([1, 2, 3, 4, 5, 6, 7, 8, 9]));
You could try using another approach, such as utilizing Set to see if there are duplicated elements:
function includes1To9(arr) {
return new Set(arr).size === arr.length;
}
console.log(includes1To9([1, 2, 1, 4, 5, 6, 7, 8, 9]));
console.log(includes1To9([1, 2, 2, 4, 5, 6, 7, 8, 9]));
console.log(includes1To9([1, 2, 3, 4, 5, 6, 7, 8, 9]));