6/6 Can't find whats wrong with my code


#1

heres my code:

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, newContact];

function printPerson(person) {
console.log(person.firstName + " " + person.lastName);
}

function list() {
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++) {
	printPerson(contacts[i]);
}
}

/*Create a search function
then call it passing "Jones"*/
function search(lastName) {
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++) {
if (contacts[i].lastName === lastName) {
printPerson(contacts[i]);
}
}

}

search()

var add = function (firstName, lastName, email, phoneNumber) {
var newContact = {
    firstName: firstName,
    lastName: lastName,
    phoneNumber: phoneNumber,
    email: email
};
};
newContact.add ("Chandler", "Dowd", "gohikearock@example.com", "918 - 555 - 555");

list();

#2

Hey there,

Ok, I'll try my best to explain this one, but if you have any questions do shout afterwords!

So the first thing I noticed, you don't want to actually add newContact to the contacts array in line 15.

The idea of the task is you are making a new friend, as a result they wouldn't be in your contacts array to begin with. So line 15 should read:

var contacts = [bob, mary];

Next, the variable add is being used to declare a function, that takes the arguments of first name, last name, email and phone number. When you want to call the function all you need to do is type:

add(enter, arguments, you'll, call)

Doing newContact.add(...) actually throws an undefined error as we haven't defined newContact previously.

So you want to drop the use of newContact.add in line 50 and have that written as:

add ("Chandler", "Dowd", "gohikearock@example.com", "918 - 555 - 555");

Even with this however, you'll still have a problem. The reason being your add function as it stands just creates a newContact object. We don't actually want it to do that. We want to push the new contact to the contacts array.

Luckily in JavaScript there is a method for that known as .push(...). So, since we want to push the object to the contacts array, instead of:

var newContact = {
    firstName: firstName,
    lastName: lastName,
    phoneNumber: phoneNumber,
    email: email
};

Your functions should call:

contacts.push({
        firstName: firstName,
        lastName: lastName,
        phoneNumber: phoneNumber,
        email: email
    });

When you've updated all the above, if you run your code you'll end up with three objects in your contacts array:

Hope that helps.


#4

Thank you so much! This helped a lot!


#5

No problem!
Glad I could help.


#6

Why did you use push, when you can easily add the object to the array?
just curious>>

  contacts[contacts.length] = {
        firstName:"Tobi", 
        lastName:"olajide",
        email:"tobi@examle.com", 
        phoneNumber:"0303"
        };

#7

i think the code should look like this

var add = function(firstName, lastName, email, phoneNumber){
contacts[contacts.length] = {
firstName:firstName,
lastName:lastName,
email:email,
phoneNumber:phoneNumber,
};
list();
}

add("Tobi", "olajide", "ola@eample.com", 40394);


#8

Good question regarding .push vs array[array.length].

I'lll explain my reasoning behind the use of the .push method, but I'm by no means a JS expert.

The reason I chose .push is two fold, first and foremost, I prefer the syntax. I found it easier to remember when starting out learning JS, and given my background is not in CS or programming of any sort making life easier for myself when learning has been my main objective.

However, the second reason I chose .push is that it has the added benefit of depending on what you're appending, it allows to append multiple items to the end of the array in an easier to write manner. Take a look at the examples below:

var ar = ['one', 'two', 'three'];
ar[ar.length] = "a", "b", "c";
   
var artwo = ['one', 'two', 'three'];
artwo.push("a","b","c");
    
var arthree = ['one', 'two', 'three'];
arthree[arthree.length] = ["a", "b", "c"];
    
var arfour = ['one', 'two', 'three'];
arfour.push(["a","b","c"]);
    
var arfive = ['one', 'two', 'three'];
arfive[arfive.length] = {a: "a", b: "b", c: "c"};
    
var arsix = ['one', 'two', 'three'];
arsix.push({a: "a", b: "b", c: "c"});

Six arrays, each passing something slightly different into the respective arrays.

The output of the above if called would be:

Now as you can see for all but the first pair, the different append approaches produce arrays are identical.

In order to get array ar to display the same as the equivalent array using .push (artwo), I would need to actually type:

var ar = ['one', 'two', 'three'];
ar[ar.length] = "a";
ar[ar.length] = "b";
ar[ar.length] = "c";

In the case of the exercise as we were simply passing an object to the contacts array, you are correct using array[array.length] = object, would work just fine. In fact, it is provided within the hint of the exercise.

But since I've never really used array[array.length] when trying to append to an array, when trying to help the OP I ended up going with what I was used to.


#9

hi avdi,

the exercise wasn't about .push method. For example I have never seen java before and would never figure out to use .push... there is another way... :+1:


#10

I get confused between function syntax. Could you breifly explain the difference between

var add = function(1,2,3,4) vs function add(1,2,3,4)

Thanks


#11

You can use a function declaration or a function expression.
FUNCTION DECLARATION:
Function name(argument){
//some code;
}

FUNCTION EXPRESSION:

This is also known as an "Anonymous function" var a = function( ) {//some code};
.Why? Because it doesn't have a name unlike the one above, and since it does not have a function name, you always invoke it with the variable name. You've basically stored your function
in "a" variable above, and thus you can use your "a" variable as a function. This function can also be used to call functions from inside functions and its helpful for recursive functions.
Most of the time, applications of this functions will be a matter of convenience.


#12

I think it's the same thing