Keep getting syntax error .. can't see it


#1



https://www.codecademy.com/courses/building-an-address-book/0/4?curriculum_id=506324b3a7dffd00020bf661#


Keep getting syntax error ... guys what I can't see ? Goal of this exercise is to get program to print out all users using a function and FOR loop.


var bob = {
    firstName: "Bob",
    lastName: "Jones",
    phoneNumber: "(650) 777-7777",
    email: "bob.jones@example.com"
};

var mary = {
    firstName: "Mary",
    lastName: "Johnson",
    phoneNumber: "(650) 888-8888",
    email: "mary.johnson@example.com"
};

var contacts = [bob, mary];

function printPerson(person) {
    console.log(person.firstName + " " + person.lastName);
}
function list() {
    var contactLength = contacts.length

    for (var i = 0, i < contactLength, i++) {
        printPerson(i);
}
}
list();


#2

uhm.. this line:

for (var i = 0, i < contactLength, i++) {

i always separate the initialize, condition and increase from each other with semi-colons, not just comma's. Also, this line:

 printPerson(i);

i is just a number:

console.log(i)
printPerson(i);

So you might want to give it a thought what to do with i


#3

Thank you mate. Appreciate your input !


#4

did you resolve the issue?


#5

Not yet. Still trying to crack it. But I'll figure it out. I always do eventually (after a few days) lol


#6

i assume you did fix your for loop? Numbers are very useful to access items inside arrays (contacts)


#7

Yeah commas are gone. I was being silly I guess. But now getting undefined ....


#8

i is still just a number. add a console.log(i) inside your for loop to see this. printPerson expects an object, not a number.

uhm.. line 20, your function doesn't have a parameter (and doesn't require one) so you don't need a argument on line 27.


#9

Code update:

Still don't like it. I'm getting green but I think I'm not supposed to have it 4 times. Gotta fix it.


#10

still, line 28 just needs to be (without argument):

list();

your function doesn't have a parameter:

function list(){//function content}

so no argument needed. Uhm.. i will hold the number? Why bother to write them manually? (line 24 and line 25)


#11

Seems like I'm tip toeing around the bushes. Feel like it's almost right. Just not yet ... lol
I'm playing with array. I need to get the program print out all the content in an array "contacts"
I figured if I don't specify it like contacts[0] or [1] it will print out everything inside. I was wrong lol


#12

why not simple use:

printPerson(contacts[i]);

i will hold the numbers you need, since the condition is i < contactsLength, which is perfect, it will give the numbers required for as many people are in your contacts array


#13

Maaan ... I feel so stupid right now ... I can see you saying "Dude just type the variable that holds the number from the FOR loop for crying out loud !"

THANK YOU. BIG TIME !


#14

But that would have been really rude to say. Always with this sort of thing, the moment you see it it makes perfect sense. I once spend 3 hours debugging a problem i had, guess what the problem was? I had a html element with a certain id, and i used the wrong id name in jquery to extract the value :open_mouth: it was tempting to throw the computer out of the window afterwards :stuck_out_tongue:


#15

lmfao !

That's a good one mate ! Yeah I get those moments too. Makes you either throw a computer out off window or pull your hair off lol !

Well I better get used to it since yesterday I enrolled on uni to study software engineering. So I better suck it up and code on. :smiley:

Listen mate you've got my big thanks for sticking with me.

Greetings from London


#16

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.