努力将错误推送到JS中的数组中



所以我得到了一些代码,并被告知要更新它,所以它不使用console.error(),而是将错误推送到错误数组中。我还需要更新代码,这样如果有任何错误,代码就会抛出一个错误数组。

给定代码:

function secretPasscode(code) {
let errors = [];
if (code.length < 14) console.error("Code is too short!");
if (code.length > 14) console.error("Code is too long!");
if (!code.includes("-")) console.error("Code is missing a '-'.");
if (code !== "jWhyYFh-eTx3qt") console.error("Code is incorrect.");
if (errors.length) {
console.error(errors);
}
return true;
}

使用errors.push("Code is too short!")可以吗?更新它以满足这两个需求的最佳方式是什么?

这是用于比较的测试文件:

const { expect } = require("chai");
const solution = require("../src/solution");
describe("Solution", () => {
describe("secretPasscode()", () => {
it("should throw an error if the given code is too short", () => {
const secretPasscode = () => solution("short-code");
const msg = "Code is too short!";
expect(secretPasscode).to.throw().with.property(0, msg);
});
it("should throw an error if the given code is too long", () => {
const secretPasscode = () => solution("very-expansive-code");
const msg = "Code is too long!";
expect(secretPasscode).to.throw().with.property(0, msg);
});
it("should throw an error if the given code does not include a dash", () => {
const secretPasscode = () => solution("codeofoklength");
const msg = "Code is missing a '-'.";
expect(secretPasscode).to.throw().with.property(0, msg);
});
it("should throw 3 errors if code is incorrect, missing dash and too short", () => {
const secretPasscode = () => solution("shortcode");
expect(secretPasscode).to.throw().have.lengthOf(3);
});
it("should throw 3 errors if code is incorrect, missing dash and too long", () => {
const secretPasscode = () => solution("veryexpansivecode");
expect(secretPasscode).to.throw().have.lengthOf(3);
});
it("should throw 2 errors if missing dash and wrong code", () => {
const secretPasscode = () => solution("codeofoklength");
expect(secretPasscode).to.throw().have.lengthOf(2);
});
it("should throw an error if the given code is simply incorrect", () => {
const secretPasscode = () => solution("pretty-ok-code");
const msg = "Code is incorrect.";
expect(secretPasscode).to.throw().with.property(0, msg);
});
it("should return true if the code is correct", () => {
const actual = solution("jWhyYFh-eTx3qt");
expect(actual).to.be.true;
});
});
});

当我使用errors.push((将错误推入数组时

然后返回失败的阵列

根据您的描述,您可以这样编码:

function secretPasscode(code) {
const errors = [];
if (code.length < 14) errors.push("Code is too short!");
if (code.length > 14) errors.push("Code is too long!");
if (!code.includes("-")) errors.push("Code is missing a '-'.");
if (code !== "jWhyYFh-eTx3qt") errors.push("Code is incorrect.");
if (errors.length) {
console.error(errors);
return false;
} else {
return true;
}
}

请注意,我还更新了从该函数返回的方式,以便调用方可以检查代码是否正常。

为了给数组添加错误,它非常简单!

我们可以在errors对象上使用push方法将我们想要的推送到数组,如下所示

errors.push("Code is too short!")

因此,对于您的代码:

function secretPasscode(code) {
let errors = [];
if (code.length < 14) errors.push("Code is too short!");
if (code.length > 14) errors.push("Code is too long!");
if (!code.includes("-")) errors.push("Code is missing a '-'.");
if (code !== "jWhyYFh-eTx3qt") errors.push("Code is incorrect.");
if (errors.length) {
console.error(errors);
return false
}
return true;
}

errors.push((应该完成这项工作,我建议为每种错误创建一个变量,并将该变量推入数组。

当您想要打印错误的堆栈跟踪时,可以对错误数组运行.map((并将错误传递给控制台.error((

希望它能有所帮助!

Nvm很傻。

推送错误很好,但我只需要将返回代码编码为

throw errors

一切都过去了!

最新更新