I am new to coding and cannot workout why my code is not working. Dose anyone have any suggestion of how to fix it?
The question I have been given is this;
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']
This is the code I have written;
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
}
My code will be run against this;
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"]);
});
});
This is the error that I am being given;
您刚刚创建了一些名称数组,您需要哈巴狗的名称,您可以使用过滤器像 pugs.filter(p => p.breed === 'pug') 这样的数组,这将为您提供一个仅包含 pug 元素的数组(检查过滤文档),而不仅仅是使用 map 从中取名,map 根据给定的回调函数转换您的数组,该函数接受数组元素并且必须返回新元素(检查 array.map)
pugs.push[i].name
is a mistake. You should have pugs.push(dogs[i].name);
.
Also, you don't really need a regex for pug
. Just check like this:
if (dogs[i].breed.toLowerCase() === "pug")
You regular expression is /(pug)/g
where it should be /(Pug)/g
. But in my opinion if don't even need to use Regex. When it's direct comparison between strings you can use the comparison operator ==
or ===
. Other issue in your code it's how you are pushing to the array. Should be pugs.push(dogs[i].name)
and not pugs.push[i].name
. Therefore this is my suggestion:
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
}
I corrected your version and created an easier version for you. Here it goes:
//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));
You had some errors.
reg.lastIndex = 0
because regex, called several times may return different resultsHope it helps you.
Within the code you wrote:
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
}
You had some typos and errors. First off, you don't really need a regular expression to check the dog breed - see my if
statement:
if (dogs[i].breed == "Pug") {...}
(You also had a typo - you spelled breed
with an a, like bread
.)
Secondly, your syntax for Array.prototype.push()
is wrong - it should be this:
pugs.push(dogs[i].name);
So after you've done all that, your code should look like this:
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;
}
However, if you really wanted to make the code smaller and faster, you could use Array.prototype.filter()
and ES6 arrow functions like so:
const getPugNames = (dogs) => dogs.filter(d => if (d.breed == "Pug") return d.name);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.