Sudoku checker not working? Can anyone help me identify the error?
P粉217784586
P粉217784586 2024-04-06 21:21:07
0
1
786

I tried everything on the online editor but still get the error. But when I do this on VSCode on my machine, it works fine. I'm confused and can't submit the code without finding the bug. I don't know where to look anymore. I wish I had another pair of eyes looking at this for me. I am truely thankful.

//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 I received. When the sudoku is invalid, the prompt asks me to return false. As you can see, it does return false for invalid puzzles, but not for 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)

P粉217784586
P粉217784586

reply all(1)
P粉301523298

Your includes1To9() only checks if adjacent elements are the same:

function includes1To9(arr) {
  let prev = arr[0];
  for (let i = 1; i 
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template