# Could anyone help me solve this problem, having a hard time wrapping my head around it

Could anyone help me solve this problem, having a hard time wrapping my head around it
0

#1

Define a function listPeople which is given an array containing objects defining people’s names and ages.

#2

So, based on the persons age, they will be given an age group. You need to determine the age group and then just list out the people with their age group instead of their age. I would start by just trying to list out all the objects in the array your given, and then go from there.

#4

so your function takes a parameter that is an array. Lets call it “arr”
for(var i=0; i<arr.length; i++){
console.log(arr[i].name, arr[i].age);
}
this will loop through the array of objects, and console log that objects “name” and “age” values.

Instead of logging them, lets add them to a string:

var str = “”; //create empty string

for(var i=0; i<arr.length; i++){
str += arr[i].name;
str += arr[i].age;
}

This will give us the Name + Age of every object in one string, and I don’t want to give any more of the answer away. If you’re still having problems after this, let me know!

#5

So this is the code I have but it doesn’t seem to work, could you please tell me what I’m missing

function listPeople(people) {
var str = “”;
for(var i = 0; i < people.length; i++) {
str += people[i].name;
str += people[i].age;
if(people[i].age <= 10 || people[i].age >= 20) {
return people[i].name + "the kid " + people[i].age + "the adult " + "& " + people[i].age + “the senior”;
}
}

}

#6

The age groups aren’t set up properly. You’ll need to look at what ages you need to account for, and then you can create an if - else if block
if( this is true ){
do this
} else if (this is true) {
do this
}
you can chain as many else if statements as you want to account for as many different circumstances as you need. Also once something is returned in a function, the function stops. So by returning that string in your if statement, you’re stopping the loop and the function. You need to build the string first, then return it after your done making it.

#7

Still having a hard time figuring it out, would you please be able to help me with the code?

#8

You’ll need to explain what it is you don’t understand,
Here is the if else documentation if thats what you’re having troubles with, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else

#10

did you change this code:
function listPeople(people) {
var str = “”;
for(var i = 0; i < people.length; i++) {
str += people[i].name;
str += people[i].age;
if(people[i].age <= 10 || people[i].age >= 20) {
return people[i].name + "the kid " + people[i].age + "the adult " + "& " + people[i].age + “the senior”;
}
}

}

if so, do you want to post it so I can see

#13

Perfect! Just maybe add a space after the ampersand at the end:
str += " & "
just for the string formatting, but its working just fine. Good job

#14

Thank you for your help!