2020-08-13 12:00:20 +02:00
|
|
|
|
---
|
|
|
|
|
id: 5e601bf95ac9d0ecd8b94afd
|
2021-03-22 07:52:28 -06:00
|
|
|
|
title: 数独求解器
|
2020-08-13 12:00:20 +02:00
|
|
|
|
challengeType: 4
|
2021-06-05 00:07:40 +09:00
|
|
|
|
forumTopicId: 462357
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: sudoku-solver
|
2020-08-13 12:00:20 +02:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
|
|
|
|
|
2021-03-22 07:52:28 -06:00
|
|
|
|
构建一个 JavaScript 的全栈应用,在功能上与这个应用相似:<https://sudoku-solver.freecodecamp.rocks/>。 可以采用下面的任意一种方式完成这个挑战:
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
2021-03-22 07:52:28 -06:00
|
|
|
|
- 克隆 [GitHub 仓库](https://github.com/freecodecamp/boilerplate-project-sudoku-solver) 并在本地完成你的项目。
|
2021-05-12 21:25:58 +05:30
|
|
|
|
- 使用[我们的 Replit 初始化项目](https://replit.com/github/freeCodeCamp/boilerplate-project-sudoku-solver)来完成你的项目。
|
2021-03-22 07:52:28 -06:00
|
|
|
|
- 使用一个你喜欢的站点生成器来完成项目。 需要确定包含了我们 GitHub 仓库的所有文件。
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
2021-03-22 07:52:28 -06:00
|
|
|
|
完成本项目后,请将一个正常运行的 demo(项目演示)托管在可以公开访问的平台。 然后在 `Solution Link` 框中提交你的项目 URL。 此外,还可以将项目的源码提交到 `GitHub Link` 中。
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
|
|
|
|
# --instructions--
|
|
|
|
|
|
2021-03-22 07:52:28 -06:00
|
|
|
|
- 所有解谜逻辑都可以进入 `/controllers/sudoku-solver.js`
|
|
|
|
|
- `validate` 函数应该使用给定的解谜字符串,然后检查它是否是 81 个有效的输入字符。
|
|
|
|
|
- `check` 函数应对棋盘的 *current* 进行验证。
|
|
|
|
|
- `solve` 函数应该处理任何给定的解谜字符串,而不仅仅是测试输入和解决方法。 你需要写出解决这个问题的逻辑。
|
|
|
|
|
- 所有路由逻辑都可以进入 `/routes/api.js`
|
|
|
|
|
- 阅读 `/controllers` 中的 `puzzle-strings.js` 文件来了解一些应用程序应该解决的示例谜题
|
|
|
|
|
- 在 `.env` 文件中将 `NODE_ENV` 设置为 `test` (没有引号),运行这个页面的挑战测试。
|
2021-05-12 21:25:58 +05:30
|
|
|
|
- 使用 `npm run test` 命令在 console 中运行测试。 按 Ctrl+Shift+P(在 Mac 上是 Cmd+Shift+P),并输入“open shell”,打开 Replit 控制台。
|
2021-03-22 07:52:28 -06:00
|
|
|
|
|
|
|
|
|
在 `tests/1_unit-tests.js` 中写下以下测试:
|
|
|
|
|
|
|
|
|
|
- 逻辑处理 81 个字符的解谜字符串
|
|
|
|
|
- 逻辑处理无效的解谜字符串 (不是 1-9 或 `.`)
|
|
|
|
|
- 逻辑处理一个长度不是 81 个字符的解谜字符串
|
|
|
|
|
- 逻辑处理有效行的位置
|
|
|
|
|
- 逻辑处理无效行的位置
|
|
|
|
|
- 逻辑处理一个有效的列位置
|
|
|
|
|
- 逻辑处理无效列位置
|
|
|
|
|
- 逻辑处理一个有效的区域 (3x3 网格)
|
|
|
|
|
- 逻辑处理一个无效的区域 (3x3 网格)
|
|
|
|
|
- 有效解谜字符串通过 solver
|
|
|
|
|
- 无效解谜字符串无法通过 solver
|
2021-06-28 20:01:36 +05:30
|
|
|
|
- Solver 返回一个不完整谜题的的预期解决方案
|
2021-03-22 07:52:28 -06:00
|
|
|
|
|
|
|
|
|
在 `tests/2_functional-tests.js` 中编写下以下测试:
|
|
|
|
|
|
|
|
|
|
- 用有效的解谜字符串解决一个谜题:POST 请求到 `/api/solve`
|
|
|
|
|
- 用缺失的解谜字符串解决一个谜题:POST 请求到 `/api/solve`
|
|
|
|
|
- 用无效字符解决一个谜题:POST 请求到 `/api/solve`
|
|
|
|
|
- 用不正确的长度解决一个谜题:POST 请求到 `/api/solve`
|
|
|
|
|
- 解决一个无法解决的谜题:POST 请求到 `/api/solve`
|
|
|
|
|
- 检查所有字段的解谜位置:POST 请求到 `/api/check`
|
|
|
|
|
- 用单个位置冲突检查解谜位置:POST 请求到 `/api/check`
|
|
|
|
|
- 检查一个有多个位置冲突的解谜位置: POST 请求到 `/api/check`
|
|
|
|
|
- 检查与所有位置冲突的解谜位置: POST 请求到 `/api/check`
|
|
|
|
|
- 检查缺失所需字段的解谜位置:POST 请求到 `/api/check`
|
|
|
|
|
- 检查一个有无效字符的解谜位置: POST 请求到 `/api/check`
|
|
|
|
|
- 检查不正确长度的解谜位置:POST 请求到 `/api/check`
|
|
|
|
|
- 检查一个无效的放置坐标的解谜位置:POST 请求到 `/api/check`
|
|
|
|
|
- 检查具有无效的放置值的解谜位置:POST 请求到 `/api/check`
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
|
2021-03-22 07:52:28 -06:00
|
|
|
|
提交自己的项目,而不是示例的 URL。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
```js
|
2021-02-06 04:42:36 +00:00
|
|
|
|
(getUserInput) => {
|
|
|
|
|
const url = getUserInput('url');
|
|
|
|
|
assert(!/.*\/sudoku-solver\.freecodecamp\.rocks/.test(getUserInput('url')));
|
|
|
|
|
};
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-22 07:52:28 -06:00
|
|
|
|
可以发送 `POST` 请求到 `/api/solve`,使用包含 `puzzle` 的表单数据这将是一个包含数字 (1-9) 和点号的字符串组合,`.` 表示空格。 返回的对象将包含一个 `solution` 属性与解决的谜题。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
```js
|
2021-02-06 04:42:36 +00:00
|
|
|
|
async (getUserInput) => {
|
|
|
|
|
const input =
|
|
|
|
|
'..9..5.1.85.4....2432......1...69.83.9.....6.62.71...9......1945....4.37.4.3..6..';
|
|
|
|
|
const output =
|
|
|
|
|
'769235418851496372432178956174569283395842761628713549283657194516924837947381625';
|
|
|
|
|
const data = await fetch(getUserInput('url') + '/api/solve', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({ puzzle: input })
|
|
|
|
|
});
|
|
|
|
|
const parsed = await data.json();
|
|
|
|
|
assert.property(parsed, 'solution');
|
|
|
|
|
assert.equal(parsed.solution, output);
|
|
|
|
|
};
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-22 07:52:28 -06:00
|
|
|
|
如果提交给 `/api/solve` 的对象缺失 `puzzle`,返回的值将是 `{ error: 'Required field missing' }`
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
```js
|
2021-02-06 04:42:36 +00:00
|
|
|
|
async (getUserInput) => {
|
|
|
|
|
const input =
|
|
|
|
|
'..9..5.1.85.4....2432......1...69.83.9.....6.62.71...9......1945....4.37.4.3..6..';
|
|
|
|
|
const output = 'Required field missing';
|
|
|
|
|
const data = await fetch(getUserInput('url') + '/api/solve', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({ notpuzzle: input })
|
|
|
|
|
});
|
|
|
|
|
const parsed = await data.json();
|
|
|
|
|
assert.property(parsed, 'error');
|
|
|
|
|
assert.equal(parsed.error, output);
|
|
|
|
|
};
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-22 07:52:28 -06:00
|
|
|
|
如果提交给 `/api/solve` 谜题包含非数字或点号的值。 返回的值将是 `{ error: 'Invalid characters in puzzle' }`
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
```js
|
2021-02-06 04:42:36 +00:00
|
|
|
|
async (getUserInput) => {
|
|
|
|
|
const input =
|
|
|
|
|
'AA9..5.1.85.4....2432......1...69.83.9.....6.62.71...9......1945....4.37.4.3..6..';
|
|
|
|
|
const output = 'Invalid characters in puzzle';
|
|
|
|
|
const data = await fetch(getUserInput('url') + '/api/solve', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({ puzzle: input })
|
|
|
|
|
});
|
|
|
|
|
const parsed = await data.json();
|
|
|
|
|
assert.property(parsed, 'error');
|
|
|
|
|
assert.equal(parsed.error, output);
|
|
|
|
|
};
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-22 07:52:28 -06:00
|
|
|
|
如果提交给 `/api/solve` 的谜题大于或小于 81 个字符, 返回的值将是 `{ error: 'Expected puzzle to be 81 characters long' }`
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
```js
|
2021-02-06 04:42:36 +00:00
|
|
|
|
async (getUserInput) => {
|
2022-02-25 00:11:18 +05:30
|
|
|
|
const inputs = [
|
|
|
|
|
'..9..5.1.85.4....2432......1...69.83.9.....6.62.71...9......1945....4.37.4.3..6.',
|
|
|
|
|
'..9..5.1.85.4....2432......1...69.83.9.....6.62.71...9......1945....4.37.4.3..6...'
|
|
|
|
|
];
|
2021-02-06 04:42:36 +00:00
|
|
|
|
const output = 'Expected puzzle to be 81 characters long';
|
2022-02-25 00:11:18 +05:30
|
|
|
|
for (const input of inputs) {
|
|
|
|
|
const data = await fetch(getUserInput('url') + '/api/solve', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({ puzzle: input })
|
|
|
|
|
});
|
|
|
|
|
const parsed = await data.json();
|
|
|
|
|
assert.property(parsed, 'error');
|
|
|
|
|
assert.equal(parsed.error, output);
|
|
|
|
|
}
|
2021-02-06 04:42:36 +00:00
|
|
|
|
};
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-22 07:52:28 -06:00
|
|
|
|
如果提交给 `/api/solve` 的谜题无效或无法解决, 返回的值将是 `{ error: 'Puzzle cannot be solved' }`
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
```js
|
2021-02-06 04:42:36 +00:00
|
|
|
|
async (getUserInput) => {
|
|
|
|
|
const input =
|
|
|
|
|
'9.9..5.1.85.4....2432......1...69.83.9.....6.62.71...9......1945....4.37.4.3..6..';
|
|
|
|
|
const output = 'Puzzle cannot be solved';
|
|
|
|
|
const data = await fetch(getUserInput('url') + '/api/solve', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({ puzzle: input })
|
|
|
|
|
});
|
|
|
|
|
const parsed = await data.json();
|
|
|
|
|
assert.property(parsed, 'error');
|
|
|
|
|
assert.equal(parsed.error, output);
|
|
|
|
|
};
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-22 07:52:28 -06:00
|
|
|
|
可以发送 `POST` 请求到 `/api/check`,包含 `puzzle`、`coordinate` 和 `value` 的对象,其中 `coordinate` 是表示行的字母 A-I,后跟表示列的数字 1-9,而 `value` 是 1-9 的数字。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
```js
|
2021-02-06 04:42:36 +00:00
|
|
|
|
async (getUserInput) => {
|
|
|
|
|
const input =
|
|
|
|
|
'..9..5.1.85.4....2432......1...69.83.9.....6.62.71...9......1945....4.37.4.3..6..';
|
|
|
|
|
const coordinate = 'A1';
|
|
|
|
|
const value = '7';
|
|
|
|
|
const data = await fetch(getUserInput('url') + '/api/check', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({ puzzle: input, coordinate, value })
|
|
|
|
|
});
|
|
|
|
|
const parsed = await data.json();
|
|
|
|
|
assert.property(parsed, 'valid');
|
|
|
|
|
assert.isTrue(parsed.valid);
|
|
|
|
|
};
|
2020-08-13 12:00:20 +02:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-22 07:52:28 -06:00
|
|
|
|
发送 `POST` 请求到 `/api/check`,返回值是一个包含 `valid` 属性的对象,如果数字可能放置在提供的坐标中则是 `true`,否则是`false`。 如果错误,返回的对象还将包含一个 `conflict` 属性,它是一个字符串 `"row"`、`"column"`, 和/或 取决于哪个区域使位置无效的`"region"` 。
|
2020-08-13 12:00:20 +02:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
2021-02-06 04:42:36 +00:00
|
|
|
|
async (getUserInput) => {
|
|
|
|
|
const input =
|
|
|
|
|
'..9..5.1.85.4....2432......1...69.83.9.....6.62.71...9......1945....4.37.4.3..6..';
|
|
|
|
|
const coordinate = 'A1';
|
|
|
|
|
const value = '1';
|
|
|
|
|
const conflict = ['row', 'column'];
|
|
|
|
|
const data = await fetch(getUserInput('url') + '/api/check', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({ puzzle: input, coordinate, value })
|
|
|
|
|
});
|
|
|
|
|
const parsed = await data.json();
|
|
|
|
|
assert.property(parsed, 'valid');
|
|
|
|
|
assert.isFalse(parsed.valid);
|
|
|
|
|
assert.property(parsed, 'conflict');
|
|
|
|
|
assert.include(parsed.conflict, 'row');
|
|
|
|
|
assert.include(parsed.conflict, 'column');
|
|
|
|
|
};
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```
|
2020-08-13 12:00:20 +02:00
|
|
|
|
|
2021-03-22 07:52:28 -06:00
|
|
|
|
如果提交给 `/api/check` 的 `value` 已放置在该 `coordinate` 上的 `puzzle`中,如果 `value` 不冲突,则返回的是 `valid` 属性为 `true` 的对象。
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
async (getUserInput) => {
|
|
|
|
|
const input =
|
|
|
|
|
'..9..5.1.85.4....2432......1...69.83.9.....6.62.71...9......1945....4.37.4.3..6..';
|
|
|
|
|
const coordinate = 'C3';
|
|
|
|
|
const value = '2';
|
|
|
|
|
const data = await fetch(getUserInput('url') + '/api/check', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({ puzzle: input, coordinate, value })
|
|
|
|
|
});
|
|
|
|
|
const parsed = await data.json();
|
|
|
|
|
assert.property(parsed, 'valid');
|
|
|
|
|
assert.isTrue(parsed.valid);
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
如果提交给 `/api/check` 的谜题包含非数字或点号的值。 返回的值将是 `{ error: 'Invalid characters in puzzle' }`
|
2020-08-13 12:00:20 +02:00
|
|
|
|
|
|
|
|
|
```js
|
2021-02-06 04:42:36 +00:00
|
|
|
|
async (getUserInput) => {
|
|
|
|
|
const input =
|
|
|
|
|
'AA9..5.1.85.4....2432......1...69.83.9.....6.62.71...9......1945....4.37.4.3..6..';
|
|
|
|
|
const coordinate = 'A1';
|
|
|
|
|
const value = '1';
|
|
|
|
|
const output = 'Invalid characters in puzzle';
|
|
|
|
|
const data = await fetch(getUserInput('url') + '/api/check', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({ puzzle: input, coordinate, value })
|
|
|
|
|
});
|
|
|
|
|
const parsed = await data.json();
|
|
|
|
|
assert.property(parsed, 'error');
|
|
|
|
|
assert.equal(parsed.error, output);
|
|
|
|
|
};
|
2020-08-13 12:00:20 +02:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-22 07:52:28 -06:00
|
|
|
|
如果提交给 `/api/check` 的谜题大于或小于 81 个字符, 返回的值将是 `{ error: 'Expected puzzle to be 81 characters long' }`
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
```js
|
2021-02-06 04:42:36 +00:00
|
|
|
|
async (getUserInput) => {
|
2022-02-25 00:11:18 +05:30
|
|
|
|
const inputs = [
|
|
|
|
|
'..9..5.1.85.4....2432......1...69.83.9.....6.62.71...9......1945....4.37.4.3..6.',
|
|
|
|
|
'..9..5.1.85.4....2432......1...69.83.9.....6.62.71...9......1945....4.37.4.3..6...'
|
|
|
|
|
];
|
2021-02-06 04:42:36 +00:00
|
|
|
|
const coordinate = 'A1';
|
|
|
|
|
const value = '1';
|
|
|
|
|
const output = 'Expected puzzle to be 81 characters long';
|
2022-02-25 00:11:18 +05:30
|
|
|
|
for (const input of inputs) {
|
|
|
|
|
const data = await fetch(getUserInput('url') + '/api/check', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({ puzzle: input, coordinate, value })
|
|
|
|
|
});
|
|
|
|
|
const parsed = await data.json();
|
|
|
|
|
assert.property(parsed, 'error');
|
|
|
|
|
assert.equal(parsed.error, output);
|
|
|
|
|
}
|
2021-02-06 04:42:36 +00:00
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-22 07:52:28 -06:00
|
|
|
|
如果提交给 `/api/check` 的对象缺失 `puzzle`、`coordinate` 或 `value`,返回的值将是 `{ error: Required field(s) missing }`
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
```js
|
|
|
|
|
async (getUserInput) => {
|
2022-02-25 00:11:18 +05:30
|
|
|
|
const inputs = [
|
|
|
|
|
{
|
|
|
|
|
puzzle: '..9..5.1.85.4....2432......1...69.83.9.....6.62.71...9......1945....4.37.4.3..6..',
|
|
|
|
|
value: '1',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
puzzle: '..9..5.1.85.4....2432......1...69.83.9.....6.62.71...9......1945....4.37.4.3..6..',
|
|
|
|
|
coordinate: 'A1',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
coordinate: 'A1',
|
|
|
|
|
value: '1'
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
for (const input of inputs) {
|
|
|
|
|
const output = 'Required field(s) missing';
|
|
|
|
|
const data = await fetch(getUserInput('url') + '/api/check', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify(input)
|
|
|
|
|
});
|
|
|
|
|
const parsed = await data.json();
|
|
|
|
|
assert.property(parsed, 'error');
|
|
|
|
|
assert.equal(parsed.error, output);
|
|
|
|
|
}
|
2021-02-06 04:42:36 +00:00
|
|
|
|
};
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-22 07:52:28 -06:00
|
|
|
|
如果提交给 `api/check` 的坐标不指向现有的网格单元格, 返回的值将是 `{ error: 'Invalid coordinate'}`
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
```js
|
2021-02-06 04:42:36 +00:00
|
|
|
|
async (getUserInput) => {
|
|
|
|
|
const input =
|
|
|
|
|
'..9..5.1.85.4....2432......1...69.83.9.....6.62.71...9......1945....4.37.4.3..6..';
|
|
|
|
|
const output = 'Invalid coordinate';
|
2022-02-25 00:11:18 +05:30
|
|
|
|
const coordinates = ['A0', 'A10', 'J1', 'A', '1', 'XZ18'];
|
2021-02-06 04:42:36 +00:00
|
|
|
|
const value = '7';
|
2022-02-25 00:11:18 +05:30
|
|
|
|
for (const coordinate of coordinates) {
|
|
|
|
|
const data = await fetch(getUserInput('url') + '/api/check', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({ puzzle: input, coordinate, value })
|
|
|
|
|
});
|
|
|
|
|
const parsed = await data.json();
|
|
|
|
|
assert.property(parsed, 'error');
|
|
|
|
|
assert.equal(parsed.error, output);
|
|
|
|
|
}
|
2021-02-06 04:42:36 +00:00
|
|
|
|
};
|
|
|
|
|
```
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-03-22 07:52:28 -06:00
|
|
|
|
如果提交给 `/api/check` 的 `value` 不是一个介于 1 到 9 之间的数字,则返回的值将是 `{ error: 'Invalid value' }`
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
async (getUserInput) => {
|
|
|
|
|
const input =
|
|
|
|
|
'..9..5.1.85.4....2432......1...69.83.9.....6.62.71...9......1945....4.37.4.3..6..';
|
|
|
|
|
const output = 'Invalid value';
|
|
|
|
|
const coordinate = 'A1';
|
2022-02-25 00:11:18 +05:30
|
|
|
|
const values = ['0', '10', 'A'];
|
|
|
|
|
for (const value of values) {
|
|
|
|
|
const data = await fetch(getUserInput('url') + '/api/check', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({ puzzle: input, coordinate, value })
|
|
|
|
|
});
|
|
|
|
|
const parsed = await data.json();
|
|
|
|
|
assert.property(parsed, 'error');
|
|
|
|
|
assert.equal(parsed.error, output);
|
|
|
|
|
}
|
2021-02-06 04:42:36 +00:00
|
|
|
|
};
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-22 07:52:28 -06:00
|
|
|
|
所有 12 个单元的测试都已完成并通过。 请参阅 `/tests/1_unit-tests.js` 来了解你应该写的测试的预期行为。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
```js
|
2021-02-06 04:42:36 +00:00
|
|
|
|
async (getUserInput) => {
|
|
|
|
|
try {
|
|
|
|
|
const getTests = await $.get(getUserInput('url') + '/_api/get-tests');
|
|
|
|
|
assert.isArray(getTests);
|
|
|
|
|
const units = getTests.filter((el) => el.context.includes('UnitTests'));
|
|
|
|
|
assert.isAtLeast(units.length, 12, 'At least 12 tests passed');
|
|
|
|
|
units.forEach((test) => {
|
|
|
|
|
assert.equal(test.state, 'passed', 'Test in Passed State');
|
|
|
|
|
assert.isAtLeast(
|
|
|
|
|
test.assertions.length,
|
|
|
|
|
1,
|
|
|
|
|
'At least one assertion per test'
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw new Error(err.responseText || err.message);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
```
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-03-22 07:52:28 -06:00
|
|
|
|
所有 14 项功能测试都已完成并通过。 请参阅 `/tests/2_functional-tests.js` 来了解你应该编写的测试的功能。
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
async (getUserInput) => {
|
|
|
|
|
try {
|
|
|
|
|
const getTests = await $.get(getUserInput('url') + '/_api/get-tests');
|
|
|
|
|
assert.isArray(getTests);
|
|
|
|
|
const funcs = getTests.filter((el) =>
|
|
|
|
|
el.context.includes('Functional Tests')
|
|
|
|
|
);
|
|
|
|
|
assert.isAtLeast(funcs.length, 14, 'At least 14 tests passed');
|
|
|
|
|
funcs.forEach((test) => {
|
|
|
|
|
assert.equal(test.state, 'passed', 'Test in Passed State');
|
|
|
|
|
assert.isAtLeast(
|
|
|
|
|
test.assertions.length,
|
|
|
|
|
1,
|
|
|
|
|
'At least one assertion per test'
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw new Error(err.responseText || err.message);
|
|
|
|
|
}
|
|
|
|
|
};
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```js
|
|
|
|
|
/**
|
|
|
|
|
Backend challenges don't need solutions,
|
|
|
|
|
because they would need to be tested against a full working project.
|
|
|
|
|
Please check our contributing guidelines to learn more.
|
|
|
|
|
*/
|
|
|
|
|
```
|