Lets me pass but doesnt print the right thing on the console


#1

this is the part of the code that we needed to write for this specific exercise.
it prints on the console "Bob Jones
Mary Jones". Howver Mary's last name is Johnson, so it should not print her out.

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

#2

This may be related to the SCT and how it checks the exercise. As long as your code is correct, and what you expect to print is printing first, you may ignore the extra output.


#3

in the if statement try this
if(lastName===contacts[i].lastName){


#4

so by changing the = to ===, i get the right result. Is that because === checks for an exact solution, while = does not? thanks!


#5

Sincle = is an assignment operator, whereas == and === are for comparisons.


#6

computer is dumb its doesnt understand if you just assign = so everytime its=== within the brackets....


#7

@mtf ya exactly you are correct....


#8

So you now can envision how an assignment works?

Eg.

LS <= RS

The Left Side becomes a reference to the object on the Right Side, whence referred will yield up that object or value.

var a = Math.PI;
console.log(a, a.toFixed(2));  // 3.141592653589793 '3.14'

We have outputted two different values from the same reference. A direct reference, a and an indirect anonymous reference, a.toFixed(2).

Notice that the indirect reference has the method referring the variable? This gives rise to a new anonymous object, a string converted from the rounded value of a set to two decimal places.

Now let's do some comparisons.

console.log(+a.toFixed(2) == a.toFixed(2));   // true
console.log(+a.toFixed(2) === a.toFixed(2));  // false

The latter is a strict type match comparison. The type on each side is different so yields false. For the former, JavaScript massages the two operands into the same type, known as type coercion so there is a possibly of equality.

==    equality
===   identity