JS如何return表示居住在一个城市的人数?
JS how to return the amount of people that live in a city?
我是编码新手,收到了这个问题,但我似乎无法使我的代码正常工作。有人有什么建议吗?
这是我得到的问题:
此函数接收一组人物对象,格式为:
[{ name: 'Sandra', lives: { country: 'UK', city: 'Manchester' }, age: 32 }]
该函数应该return居住在瓦伦西亚市的人数
这是我编写的代码;
function countPeopleInValencia(people) {
let count = 0
for (let i = 0; i < people.length; i++) {
if (people.city[i] === 'Valencia') {
count ++ }
else {return 0}
}
return count
}
这就是我的代码运行反对的内容;
describe("countPeopleInValencia", () => {
it("returns 0 when nobody is from Valencia", () => {
expect(
countPeopleInValencia([
{
name: "Sandra",
lives: { country: "UK", city: "Manchester" },
age: 32
},
{
name: "Sandrella",
lives: { country: "Spain", city: "Bilbao" },
age: 32.5
}
])
).to.equal(0);
});
it("returns the length of the array when everyone is from Valencia", () => {
expect(
countPeopleInValencia([
{
name: "Cassandra",
lives: { country: "Spain", city: "Valencia" },
age: 32.5
},
{
name: "Cassandrella",
lives: { country: "Spain", city: "Valencia" },
age: 35.55
}
])
).to.equal(2);
});
it("returns the number of people who are actually from the fair city of Valencia", () => {
expect(
countPeopleInValencia([
{
name: "Melissandra",
lives: { country: "Spain", city: "Valencia" },
age: 55.5
},
{
name: "Melissandrella",
lives: { country: "Spain", city: "Valencia" },
age: 55.555
},
{
name: "Keith",
lives: { country: "UK", city: "Newport Pagnell" },
age: 2
}
])
).to.eql(2);
expect(
countPeopleInValencia([
{
name: "Imeldarina",
lives: { country: "Spain", city: "Valencia" },
age: 15.2
},
{
name: "Bob",
lives: { country: "Wales", city: "Abertillery" },
age: 555555555555.555
},
{
name: "Terry",
lives: { country: "England", city: "Newport Pagnell" },
age: 0.00000002
}
])
).to.equal(1);
});
});
在您当前的代码中,您 return 一旦找到不在您要计算的城市的人,您就会计算值
function countPeopleInValencia(people) {
let count = 0
for (let i = 0; i < people.length; i++) {
if (people.city[i] === 'Valencia') {
count ++
}
}
return count
}
除了将计数移到 for 循环之外,您还应该更正
people.city[i]
至
people[i]['lives']['city']
你的函数:
function countPeopleInValencia(people) {
let count = 0
for (let i = 0; i < people.length; i++) {
if (people[i]['lives']['city'] === 'Valencia') {
count++;
}
}
return count
}
我是编码新手,收到了这个问题,但我似乎无法使我的代码正常工作。有人有什么建议吗?
这是我得到的问题:
此函数接收一组人物对象,格式为:
[{ name: 'Sandra', lives: { country: 'UK', city: 'Manchester' }, age: 32 }]
该函数应该return居住在瓦伦西亚市的人数
这是我编写的代码;
function countPeopleInValencia(people) {
let count = 0
for (let i = 0; i < people.length; i++) {
if (people.city[i] === 'Valencia') {
count ++ }
else {return 0}
}
return count
}
这就是我的代码运行反对的内容;
describe("countPeopleInValencia", () => {
it("returns 0 when nobody is from Valencia", () => {
expect(
countPeopleInValencia([
{
name: "Sandra",
lives: { country: "UK", city: "Manchester" },
age: 32
},
{
name: "Sandrella",
lives: { country: "Spain", city: "Bilbao" },
age: 32.5
}
])
).to.equal(0);
});
it("returns the length of the array when everyone is from Valencia", () => {
expect(
countPeopleInValencia([
{
name: "Cassandra",
lives: { country: "Spain", city: "Valencia" },
age: 32.5
},
{
name: "Cassandrella",
lives: { country: "Spain", city: "Valencia" },
age: 35.55
}
])
).to.equal(2);
});
it("returns the number of people who are actually from the fair city of Valencia", () => {
expect(
countPeopleInValencia([
{
name: "Melissandra",
lives: { country: "Spain", city: "Valencia" },
age: 55.5
},
{
name: "Melissandrella",
lives: { country: "Spain", city: "Valencia" },
age: 55.555
},
{
name: "Keith",
lives: { country: "UK", city: "Newport Pagnell" },
age: 2
}
])
).to.eql(2);
expect(
countPeopleInValencia([
{
name: "Imeldarina",
lives: { country: "Spain", city: "Valencia" },
age: 15.2
},
{
name: "Bob",
lives: { country: "Wales", city: "Abertillery" },
age: 555555555555.555
},
{
name: "Terry",
lives: { country: "England", city: "Newport Pagnell" },
age: 0.00000002
}
])
).to.equal(1);
});
});
在您当前的代码中,您 return 一旦找到不在您要计算的城市的人,您就会计算值
function countPeopleInValencia(people) {
let count = 0
for (let i = 0; i < people.length; i++) {
if (people.city[i] === 'Valencia') {
count ++
}
}
return count
}
除了将计数移到 for 循环之外,您还应该更正
people.city[i]
至
people[i]['lives']['city']
你的函数:
function countPeopleInValencia(people) {
let count = 0
for (let i = 0; i < people.length; i++) {
if (people[i]['lives']['city'] === 'Valencia') {
count++;
}
}
return count
}