Is this supposed to happen?


#1

When I key in this code, I will "pass" the exercise, but the output looks wrong.

var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "6123 5598",
        address: ['One Microsoft Way','Redmond','WA','98052']
    },
    steve: {
        firstName: "Steve",
        lastName: "Jobs",
        number: "1800-692-7753",
        address: ['1 Infinite Loop', 'Cupertino', 'California']
    }
};

var list = function(friends) {
    for (var firstName in friends) {
        console.log(firstName);
    }
};

var search = function(name) {
    for (var property in friends) {
        if (friends[property].firstName === name) {
            console.log(friends[property]);
            return friends[property];
        } else {
            console.log("no information!");
        }
    }
};

The output is:

no information!
{ firstName: 'Steve',
  lastName: 'Jobs',
  number: '1800-692-7753',
  address: [ '1 Infinite Loop', 'Cupertino', 'California' ] }
no information!
{ firstName: 'Steve',
  lastName: 'Jobs',
  number: '1800-692-7753',
  address: [ '1 Infinite Loop', 'Cupertino', 'California' ] }
{ firstName: 'Bill',
  lastName: 'Gates',
  number: '6123 5598',
  address: [ 'One Microsoft Way', 'Redmond', 'WA', '98052' ] }

If it's something that's not supposed to happen, how do I fix it?


#2

for validition purposes, the exercise also calls the functions, this causes output, don't worry about it. See what happens when you run it on jsbin for example, make sure to open JS and console tab


#3

So I used Visual Studio Code, with an extra code* it showed this:

*extra code here:

search("Bill");
search("Steve");

Technically, the "no information!" shouldn't show up at all?


#4

then place no information outside your loop? currently, for everytime if evaluates to false, no information will be logged


#5

Thank you very much. It works when I put it outside of the 'if/else" statement. :slight_smile:


#6

this means you should be able to get rid off else, given you placed it outside the loop


#7

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