6/6 why can't use "this" keyword?


#1

hello guys
here the wrong 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];

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 numberOfItems=contacts.length;
for(a = 0; a < numberOfItems; a++){
    if(lastName===contacts[a].lastName){
        printPerson(contacts[a])
        break;
    }
    else{
        console.log("not found")
    }
}
}

function add(firstName,lastName,email,phoneNumber){
this.firstName=firstName;
this.lastName=lastName;
this.email=email;
this.phoneNumber=phoneNumber;
};
contacts[contacts.length]=add();
add("abc","def","abc@example.com","1234567890");
list();

it say TypeError: Cannot read property 'firstName' of undefined

i want to know why i can't use "this" keyword in the add function and then make contacts[contacts.length]= add ??
can anyone explain this for me please ??


#3

Since you have written a constructor function, you need to invoke it with new so it returns a new object.

And since you are pushing the envelope, suggest use Array.push().

contacts.push(new add("abc","def","abc@example.com","1234567890"));

But then the question comes up... What the heck is an add object? We've just created a new class of object that is not very semantic. Now the new object has a class (other than Object) when the others in the array do not.

There is an issue with your search function. There should not be an else statement inside the loop. Given a larger database, if a name is passed in that is is not found, the output to the screen will look like,

not found
not found
not found
not found
not found
not found
not found
not found

Not the output we would expect. Remove,

else {
    console.log("not found")
}

The instructions ask us to return the found object.

If you don't want your code to be confusing to read or debug, use generic variables, not the property names found in the objects. And it is not necessary to cache the length in simple constructs like this.

var search = function (name) {
    for (var a = 0; a < contacts.length; a++) {
        if (contacts[a].firstName === name) {
            printPerson(contacts[a]);
            return contacts[a];
        }
    }
};

#4

thanks for your answer i used push and it worked but what if i want to add the new person object in a specific place of the array ?? is there anyway to do this using the same function syntax or i have to define the place in the function body ??

about my search function i thought every if statement should end with else otherwise the compiler would say unexpected end of input that's why i used else ,, if i removed else and passed a name that doesn't exist what will be the output ??
and last question is it right to use break inside if statement or i should use it in (switch) only ??
thanks again


#5

@amired
Please read
https://discuss.codecademy.com/t/6-6-difference-with-code/8871/3?u=leonhard.wettengmx.n
and
https://discuss.codecademy.com/t/6-6-difference-with-code/8871/4?u=leonhard.wettengmx.n


#6

This would be the tool to use:

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

Not the case. The else clause is a default action (if there is to be one). It is not required in the normal sense unless there is to be a default action. In this case there is none. The return value will be undefined if no matches are found.

Not unless the if statement is in a loop.. break is intended for use in loop constructs such as, for () {}, while () {}, and do {} while ();. As you already noted, switch () {} which is not a loop but if return is not used (switch is not in a function) it prevents fall through.


#7

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