创建一个函数,该函数接受一组人物对象并从数组中返回第一个找到的宇航员对象.
这是我创建的代码;
function findFirstAstronaut(people) {
for (let i = 0; i < people.length; i++) {
if (people.astronaut[i] === false) {
return null
}
else if (people.astronaut[i]) {
return people[i]
}
}
Run Code Online (Sandbox Code Playgroud)
我的代码是针对此测试运行的;
describe("findFirstAstronaut", () => {
it("returns null if no Astronaut is in the array", () => {
expect(findFirstAstronaut([])).to.be.null;
});
it("returns a person object who is an astronaut", () => {
const astronaut = { name: "Tim Peake", isAstronaut: true };
expect(findFirstAstronaut([astronaut])).to.have.keys([
"name",
"isAstronaut"
]);
expect(findFirstAstronaut([astronaut]).isAstronaut).to.be.true;
});
it("returns the first …Run Code Online (Sandbox Code Playgroud) 我是编码的新手,并且已经给出了我似乎无法做到的这个问题:
创建一个带有正整数数组的函数,并返回这些数字的阶乘数组.
__PRE__
数字的阶乘是该数字与其下方所有整数的乘积.
__PRE__
如果该数字小于0,则拒绝它.
我创建的代码是这样的;
__PRE__
代码正在针对此测试运行;
__PRE__
我该怎么做?
我是编码新手,曾被问过这个问题;
创建一个将接收手机号码字符串的函数。如果该号码是有效的英国号码,则应返回 true,否则返回 false。
有效的手机号码可能以“07”开头,后跟 9 位数字。它也可能以“+447”开头,后跟 9 个数字。它也可能以“00447”开头,后跟 9 个数字。其他的都是无效的。
这是我创建的代码;
function validMobileNumber(num) {
if (num.length != 9)
{
return false
}
if (num.length = 9 && num.includes(07, 0) && num.includes(+447, 0) && num.includes(00447, 0)) {
return true
}
}
Run Code Online (Sandbox Code Playgroud)
我的代码将针对此运行;
describe("validMobileNumber", () => {
it("returns false when passed a string of the wrong length", () => {
expect(validMobileNumber("123")).to.equal(false);
expect(validMobileNumber("0750617250638")).to.equal(false);
expect(validMobileNumber("+447712368768724988")).to.equal(false);
});
it("returns true when passed a valid plain phone num string", () => {
expect(validMobileNumber("07506172506")).to.equal(true);
});
it("returns true …Run Code Online (Sandbox Code Playgroud)