5 . In the If statement ... Shouldn't we use the comparing operator (===) instead of the declaring operator (=)?


#1


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


I have passed the challenge already , but what Iam asking about is why are we using the assignment (=) operator in the if statement instead of the comparing operator (===) ? I mean it just looks a bit illogical to me
+ I experimented the code in an external environment (using komodo edit and google chrome console) , the editor gave me syntax error in the part if statement part that I mentioned earlier , but still the code behaved right when it was written with the assignment operator (=) and didn't behave right when i fixed the error and wrote the comparing operator (===)
in the google chrome console , So how can we explain this ?

Iam so sorry for the long issue but i didn't make sense to me and i liked to share it here with you all .


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"*/
var search = function (lastName) {
    var contactsLength = contacts.length;
    for (var i = 0 ; i < contactsLength ; i++) {
      /* this is the part iam asking about */
        if (contacts[i].lastName = lastName) {
            printPerson(contacts[i]);
            };
        };
    };
search("jones");


#2

Hi,

I'm a newb at coding, but you are supposed to use the comparative operator ===, even if the assignment operator = allows you to pass the course. (Might be an oversight on Codecademy).

If you use = instead of ===, you just change the last name of everyone in the address book. Test it out.

If you use ===, then searching for Jones should only print Bob Jones.

I don't have a real explanation beyond I think it's a bug in Codecademy.


#3

Hello sir , and thanks for your consideration , as I mentioned earlier :

so I already tested the code in an external environment , and the code didn't behave right when I used the comparison operator (===) but it behaved right when I used the assignment operator (=) , so maybe i guess it isn't a bug in codeacademy .


#4

I've been having the same problem. I pasted my code into the JS environment on Khanacademy.org (https://www.khanacademy.org/computer-programming/new/pjs) and it seemed to run fine when using the comparison operator ===. Note: If you want to give it a go in their environment you need to use the following conventions for declaring functions - var variableName = function() {};

Maybe it's just a weird bug with some environments.

Anyway I hope this helps.


#5

Oh and also, the Khanacademy environment used println(); instead of console.log();


#6

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