refactoring formatting
This commit is contained in:
@@ -185,7 +185,7 @@ class SudokuSolver {
|
|||||||
// transform character array into
|
// transform character array into
|
||||||
// 2-d array with each element representing a row
|
// 2-d array with each element representing a row
|
||||||
// that contains an array of column values
|
// that contains an array of column values
|
||||||
let generateBoard = (puzzleValues) => {
|
let createBoard = (puzzleValues) => {
|
||||||
|
|
||||||
let board = [[], [], [], [], [], [], [], [], []]
|
let board = [[], [], [], [], [], [], [], [], []]
|
||||||
let boardRow = -1
|
let boardRow = -1
|
||||||
@@ -211,7 +211,7 @@ class SudokuSolver {
|
|||||||
return board;
|
return board;
|
||||||
}
|
}
|
||||||
|
|
||||||
let canPlace = (board, row, col, value) => {
|
let checkPlacement = (board, row, col, value) => {
|
||||||
|
|
||||||
// verify that the value can be
|
// verify that the value can be
|
||||||
// placed in the given column
|
// placed in the given column
|
||||||
@@ -256,7 +256,7 @@ class SudokuSolver {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let solveFromCell = (board, row, col) => {
|
let solveForGivenCell = (board, row, col) => {
|
||||||
|
|
||||||
// reset column and increment
|
// reset column and increment
|
||||||
// row when end of row reached
|
// row when end of row reached
|
||||||
@@ -279,7 +279,7 @@ class SudokuSolver {
|
|||||||
// next cell
|
// next cell
|
||||||
if (board[row][col] != '.') {
|
if (board[row][col] != '.') {
|
||||||
|
|
||||||
return solveFromCell(board, row, col + 1);
|
return solveForGivenCell(board, row, col + 1);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -291,11 +291,11 @@ class SudokuSolver {
|
|||||||
|
|
||||||
let valueToPlace = i.toString();
|
let valueToPlace = i.toString();
|
||||||
|
|
||||||
if (canPlace(board, row, col, valueToPlace)) {
|
if (checkPlacement(board, row, col, valueToPlace)) {
|
||||||
|
|
||||||
board[row][col] = valueToPlace;
|
board[row][col] = valueToPlace;
|
||||||
|
|
||||||
if (solveFromCell(board, row, col + 1) != false) {
|
if (solveForGivenCell(board, row, col + 1) != false) {
|
||||||
|
|
||||||
return board;
|
return board;
|
||||||
|
|
||||||
@@ -313,8 +313,8 @@ class SudokuSolver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get the solution for the given puzzle
|
// get the solution for the given puzzle
|
||||||
let board = generateBoard(puzzle);
|
let board = createBoard(puzzle);
|
||||||
let solution = solveFromCell(board, 0, 0);
|
let solution = solveForGivenCell(board, 0, 0);
|
||||||
|
|
||||||
if (solution == false) return false;
|
if (solution == false) return false;
|
||||||
return solution.flat().join('');
|
return solution.flat().join('');
|
||||||
|
|||||||
@@ -11,46 +11,56 @@ const runner = require('./test-runner');
|
|||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
app.use('/public', express.static(process.cwd() + '/public'));
|
app.use('/public', express.static(process.cwd() + '/public'));
|
||||||
app.use(cors({ origin: '*' })); //For FCC testing purposes only
|
app.use(cors({ origin: '*' }));
|
||||||
|
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
app.use(bodyParser.urlencoded({ extended: true }));
|
app.use(bodyParser.urlencoded({ extended: true }));
|
||||||
|
|
||||||
//Index page (static HTML)
|
app.route('/').get((req, res) => {
|
||||||
app.route('/')
|
|
||||||
.get(function (req, res) {
|
|
||||||
res.sendFile(process.cwd() + '/views/index.html');
|
|
||||||
});
|
|
||||||
|
|
||||||
//For FCC testing purposes
|
res.sendFile(process.cwd() + '/views/index.html');
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
apiRoutes(app);
|
||||||
fccTestingRoutes(app);
|
fccTestingRoutes(app);
|
||||||
|
|
||||||
// User routes
|
|
||||||
apiRoutes(app);
|
|
||||||
|
|
||||||
//404 Not Found Middleware
|
//404 Not Found Middleware
|
||||||
app.use(function (req, res, next) {
|
app.use((req, res, next) => {
|
||||||
|
|
||||||
res.status(404)
|
res.status(404)
|
||||||
.type('text')
|
.type('text')
|
||||||
.send('Not Found');
|
.send('Not Found');
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
//Start our server and tests!
|
|
||||||
const PORT = process.env.PORT || 3000
|
const PORT = process.env.PORT || 3000
|
||||||
app.listen(PORT, function () {
|
|
||||||
|
app.listen(PORT, () => {
|
||||||
|
|
||||||
console.log("Listening on port " + PORT);
|
console.log("Listening on port " + PORT);
|
||||||
// process.env.NODE_ENV='test'
|
|
||||||
if (process.env.NODE_ENV === 'test') {
|
if (process.env.NODE_ENV === 'test') {
|
||||||
|
|
||||||
console.log('Running Tests...');
|
console.log('Running Tests...');
|
||||||
setTimeout(function () {
|
|
||||||
|
setTimeout(() => {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
runner.run();
|
runner.run();
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
||||||
console.log('Tests are not valid:');
|
console.log('Tests are not valid:');
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}, 3500);
|
}, 3500);
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = app; // for testing
|
module.exports = app; // for testing
|
||||||
Reference in New Issue
Block a user