我是编码新手,无法锻炼为什么我的代码无法正常工作。剂量有人建议如何解决吗?
我得到的问题是这个;
This function takes an array of dog objects and returns an array of the names of all the pugs.
E.g. [
{name: 'Beatrice', breed: 'Lurcher'},
{name: 'Max', breed: 'Pug'},
{name: 'Poppy', breed: 'Pug'}
]
will return ['Max', 'Poppy']
这是我编写的代码;
function getPugNames(dogs) {
let pugs = []
let reg = /(pug)/g
for (let i = 0; i < dogs.length; i ++) {
if(reg.test(dogs[i].bread)) {
pugs.push[i].name
}
} return pugs
}
我的代码将与此相对应;
describe("getPugNames", () => {
it("returns [] when passed []", () => {
expect(getPugNames([])).to.eql([]);
});
it("returns an array of pug names when passed an array of dog objects", () => {
const dogs = [
{ name: "Beatrice", breed: "Lurcher" },
{ name: "Max", breed: "Pug" },
{ name: "Poppy", breed: "Pug" }
];
expect(getPugNames(dogs)).to.eql(["Max", "Poppy"]);
const dogs2 = [
{ name: "Steven", breed: "Lurcher" },
{ name: "Daphne", breed: "Pug" },
{ name: "Sandy", breed: "Labrador" },
{ name: "Mike", breed: "Pug" },
{ name: "Spike", breed: "Pug" }
];
expect(getPugNames(dogs2)).to.eql(["Daphne", "Mike", "Spike"]);
const dogs3 = [
{ name: "Kevin", breed: "Labrador" },
{ name: "Patch", breed: "Rottweiler" },
{ name: "Miles", breed: "Lurcher" },
{ name: "Sandy", breed: "Pug" },
{ name: "Spot", breed: "Pug" },
{ name: "Josephine", breed: "Terrier" },
{ name: "Eric", breed: "Pug" }
];
expect(getPugNames(dogs3)).to.eql(["Sandy", "Spot", "Eric"]);
});
});
这是我得到的错误;
答案 0 :(得分:0)
您刚刚创建了一些名称数组,您需要使用pugs命名,您可以像pugs.filter(p => p.breed ==='pug')这样使用数组过滤器,这只会为您提供一个带有pug的数组elements(检查过滤器文档),而不仅仅是使用map命名,map根据给定的回调函数转换数组,该回调函数接受数组元素并且必须返回新元素(check array.map)
答案 1 :(得分:0)
pugs.push[i].name
是一个错误。您应该有pugs.push(dogs[i].name);
。
此外,您实际上并不需要pug
的正则表达式。只需像这样检查:
if (dogs[i].breed.toLowerCase() === "pug")
答案 2 :(得分:0)
您的正则表达式为/(pug)/g
,应为/(Pug)/g
。但是我认为,甚至不需要使用Regex。当直接在字符串之间进行比较时,可以使用比较运算符==
或===
。
代码中的另一个问题是您如何推入数组。应该是pugs.push(dogs[i].name)
,而不是pugs.push[i].name
。因此,这是我的建议:
function getPugNames(dogs) {
let pugs = []
for (let i = 0; i < dogs.length; i ++) {
if(dogs[i].bread === 'Pug') {
pugs.push(dogs[i].bread)
}
}
return pugs
}
答案 3 :(得分:0)
我更正了您的版本,并为您创建了更简单的版本。在这里:
//corrected version
function getPugNames(dogs) {
var pugs = [];
var reg = /pug/gi
for (let i = 0; i < dogs.length; i ++) {
reg.lastIndex = 0;
if (reg.test(dogs[i].breed))
pugs.push(dogs[i].name);
};
return pugs
}
//easier version:
var getPugNames2 = dogs=>dogs
.map(d=>d.breed.toLowerCase() == "pug" ? d.name : null)
.filter(e=>e);
const dogs1 = [
{ name: "Beatrice", breed: "Lurcher" },
{ name: "Max", breed: "Pug" },
{ name: "Poppy", breed: "Pug" }
];
const dogs2 = [
{ name: "Steven", breed: "Lurcher" },
{ name: "Daphne", breed: "Pug" },
{ name: "Sandy", breed: "Labrador" },
{ name: "Mike", breed: "Pug" },
{ name: "Spike", breed: "Pug" }
];
const dogs3 = [
{ name: "Kevin", breed: "Labrador" },
{ name: "Patch", breed: "Rottweiler" },
{ name: "Miles", breed: "Lurcher" },
{ name: "Sandy", breed: "Pug" },
{ name: "Spot", breed: "Pug" },
{ name: "Josephine", breed: "Terrier" },
{ name: "Eric", breed: "Pug" }
];
console.log(getPugNames([]));
console.log(getPugNames(dogs1));
console.log(getPugNames(dogs2));
console.log(getPugNames(dogs3));
console.log(getPugNames2([]));
console.log(getPugNames2(dogs1));
console.log(getPugNames2(dogs2));
console.log(getPugNames2(dogs3));
您遇到了一些错误。
reg.lastIndex = 0
,因为多次调用的正则表达式可能会返回不同的结果希望它对您有帮助。
答案 4 :(得分:0)
在您编写的代码中:
function getPugNames(dogs) {
let pugs = []
let reg = /(pug)/g
for (let i = 0; i < dogs.length; i ++) {
if(reg.test(dogs[i].bread)) {
pugs.push[i].name
}
} return pugs
}
您有一些错别字和错误。首先,您真的不需要正则表达式来检查狗的品种-请参阅我的if
声明:
if (dogs[i].breed == "Pug") {...}
(您也有错字-您用{拼写了breed
,例如bread
。)
第二,您对Array.prototype.push()
的语法是错误的-应该是这样的:
pugs.push(dogs[i].name);
因此,在完成所有这些操作之后,您的代码应如下所示:
function getPugNames(dogs) {
let pugs = [];
for (let i = 0; i < dogs.length; i++) {
if (dogs[i].breed == "Pug") {
pugs.push(dogs[i].name);
}
}
return pugs;
}
但是,如果您真的想使代码更小,更快,则可以像这样使用Array.prototype.filter()
和ES6 arrow functions:
const getPugNames = (dogs) => dogs.filter(d => if (d.breed == "Pug") return d.name);