Same code, different result?


#1


https://www.codecademy.com/en/courses/javascript-beginner-en-3bmfN/0/8?curriculum_id=506324b3a7dffd00020bf661


In both exercises 7 and 8, code is accepted; however, I noticed that the printout/return in exercise 7, is much longer than in exercise 8. Why does the same code, printout/return different results?

Exercise 7 result:

bill
steve
{ firstName: 'Steve',
lastName: 'Ballcot',
number: '(206) 910-7077',
address: [ '1964 Eighty Eight St', 'Fairmont', 'LA', '00156' ] }
{ firstName: 'Steve',
lastName: 'Ballcot',
number: '(206) 910-7077',
address: [ '1964 Eighty Eight St', 'Fairmont', 'LA', '00156' ] }
{ firstName: 'Steve',
lastName: 'Ballcot',
number: '(206) 910-7077',
address: [ '1964 Eighty Eight St', 'Fairmont', 'LA', '00156' ] }
{ firstName: 'Bill',
lastName: 'Morrison',
number: '(415) 206-2213',
address: [ '381 First Street', 'Oakland', 'CA', '94153' ] }
{"firstName":"Steve","lastName":"Ballcot","number":"(206) 910-7077","address":["1964 Eighty Eight St","Fairmont","LA","00156"]}

Exercise 8 result:

bill
steve
{ firstName: 'Bill',
lastName: 'Morrison',
number: '(415) 206-2213',
address: [ '381 First Street', 'Oakland', 'CA', '94153' ] }
{"firstName":"Bill","lastName":"Morrison","number":"(415) 206-2213","address":["381 First Street","Oakland","CA","94153"]}

var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Morrison",
        number: "(415) 206-2213",
        address: ['381 First Street', 'Oakland', 'CA', '94153']
    },
    
    steve: {
        firstName: "Steve",
        lastName: "Ballcot",
        number: "(206) 910-7077",
        address: ['1964 Eighty Eight St', 'Fairmont', 'LA', '00156']
    },
};

var list = function(obj) {
    for(var info in obj) {
    console.log(info);
    }
};

var search = function(name) {
    for(var data in friends) {
        if(friends[data].firstName === name) {
            console.log(friends[data]);
            return friends[data];
        }
    }
}; 

list(friends);
search("Bill");


#2

for validation the function is called, in exercise 7 this happens more (multiply function calls) then in exercise 8, causing more output in exercise 7.


#3

But the code is identical (I typed the above code in part 7 and also in part 8, line for line). So how are there more function calls, if the code is the same?


#4

well, the codes needs to be validated, the checking script contains function calls


#5

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