Rumah > Soal Jawab > teks badan
Saya mencuba segala-galanya pada editor dalam talian tetapi masih mendapat ralat. Tetapi apabila saya melakukan ini pada VSCode pada mesin saya, ia berfungsi dengan baik. Saya keliru dan tidak boleh menyerahkan kod tanpa mencari pepijat. Tak tahu nak cari kat mana lagi. Saya harap saya mempunyai sepasang mata lagi melihat ini untuk saya. Saya benar-benar bersyukur.
//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
Ini ialah mesej ralat yang saya terima. Apabila sudoku tidak sah, gesaan meminta saya mengembalikan palsu. Seperti yang anda lihat, ia mengembalikan palsu untuk teka-teki yang tidak sah, tetapi bukan untuk semua teka-teki yang tidak sah.
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粉3015232982024-04-07 11:16:55
includes1To9()
anda hanya menyemak sama ada elemen bersebelahan adalah sama:
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]));