JSFiddle - React, Tailwind, and code Playground

by Pankaj Parkar

JavaScript

var soduko = [
  ["5", "3", ".", ".", "7", ".", ".", ".", "."],
  ["6", ".", ".", "1", "9", "5", ".", ".", "."],
  [".", "9", "8", ".", ".", ".", ".", "6", "."],
  ["8", ".", ".", ".", "6", ".", ".", ".", "3"],
  ["4", ".", ".", "8", ".", "3", ".", ".", "1"],
  ["7", ".", ".", ".", "2", ".", ".", ".", "6"],
  [".", "6", ".", ".", ".", ".", "2", "8", "."],
  [".", ".", ".", "4", "1", "9", ".", ".", "5"],
  [".", ".", ".", ".", "8", ".", ".", "7", "9"]
];

function isValidLine(line) {
  const set = new Set();
  const result = line.filter((l) => l !== '.');
  return result.length === [...new Set(result)].length;
}

function checkIfBoxValid(matrixSize) {
  const startingPoints = [];

}

function isValidSodoku(matrix) {
  const map = new Map([]);
  // loop over each item of row
  for (let i = 0; i < matrix.length; i++) {
    const row = matrix[i];
    if (!isValidLine(row)) return false;
    
    // check columns
    const cols = [];
    for (let j = 0; j < matrix.length; j++) {
       cols.push(row[j]);
    }
    console.log(cols)
    if (!isValidLine(cols)) return false;
    
  }
  return true;
}


console.log(isValidSodoku(soduko));