deathprofessor-js-lab1
Version:
This is the best lab1 ever!
52 lines • 1.46 kB
JavaScript
class Lab1 {
task1(str = "") {
return str
.toLowerCase()
.split("")
.map((char, index, array) => array.indexOf(char) === array.lastIndexOf(char) ? "(" : ")")
.join("");
}
task2(data) {
return (function () {
let _rows, _cols, _grid;
const init = function (data) {
_reorganizeData(data);
return this;
};
const isValid = function () {
return (_validate(_rows) && _validate(_cols) && _validate(_grid));
};
const _validate = function (data) {
for (let row = 0; row < 9; row++) {
data[row].sort();
for (let col = 0; col < 9; col++) {
const value = data[row][col], next_value = data[row][col + 1];
if (!(value && value > 0 && value < 10)) return false;
if (col !== 8 && value === next_value) return false;
}
}
return true;
};
const _reorganizeData = function (data) {
_rows = data;
_cols = [];
_grid = [];
for (let i = 0; i < 9; i++) {
_cols.push([]);
_grid.push([]);
}
for (let row = 0; row < 9; row++) {
for (let col = 0; col < 9; col++) {
_cols[col][row] = data[row][col];
const gridRow = Math.floor(row / 3);
const gridCol = Math.floor(col / 3);
const gridIndex = gridRow * 3 + gridCol;
_grid[gridIndex].push(data[row][col]);
}
}
};
return { init: init, isValid: isValid };
})().init(data).isValid();
}
}
module.exports = Lab1;