What would a code for this look like?

Hello everyone, I need to make a function that will display certian text using For loop. Can anyone help me?

const students = [
{ name: “John”, city: “New York” },
{ name: “Peter”, city: “Paris”},
{ name: “Kate”, city: “Sidney” }
]

I need to have console.log to display "<Name> lives in <city>"

Hey there,

what’s your guess how to solve it?

1 Like

Hi,

I was going for

for(let i = 0; i  < students.length; i++){
console.log(students.name[i] + " "+ "lives in" + " " + students.city[i]);

Good first try.

So what does the error say?
And why?

I got "Uncaught SyntaxError: Invalid or unexpected token "

And I have no idea why :sweat_smile:

Mostly caused by a wrong bracket.

Did you close all open brackets like () and {}?

Now I got this “TypeError: Cannot read property ‘0’ of undefined”

Nice!

So there’s this thing students.name[i].

What do you want to do there?

2 Likes

I want to select the value of the each name key each time

1 Like

I like how you are allowing him to figure it out :slight_smile:

2 Likes

How do you select an element in an array?

1 Like

So how can you access one specific student?

2 Likes

You forgot }
for(let i = 0; i < students.length; i++){
console.log(students.name[i] + " "+ “lives in” + " " + students.city[i]);

1 Like

He has fixed that :slight_smile:

Hey if you don’t know say you don’t know. We are trying to help you :slight_smile:

2 Likes

students[0] for the first one and so on for the rest?

1 Like

I have figured it out now, I’ve placed [i] at the wrong position, thanks a lot!

2 Likes

Glad you managed to solve it! :clap:

1 Like