0

I'm working through freecodecamp and am on the following excercise:

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup

Now my code is as follows:

var contacts = [{
  "firstName": "Akira",
  "lastName": "Laine",
  "number": "0543236543",
  "likes": ["Pizza", "Coding", "Brownie Points"]
}, {
  "firstName": "Harry",
  "lastName": "Potter",
  "number": "0994372684",
  "likes": ["Hogwarts", "Magic", "Hagrid"]
}, {
  "firstName": "Sherlock",
  "lastName": "Holmes",
  "number": "0487345643",
  "likes": ["Intriguing Cases", "Violin"]
}, {
  "firstName": "Kristian",
  "lastName": "Vos",
  "number": "unknown",
  "likes": ["Javascript", "Gaming", "Foxes"]
}];


function lookUpProfile(name, prop) {
  // Only change code below this line
  // Only change code below this line

  for (var i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName == name) {
      foundName += 1;
    }
    if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop)) {
      return contacts[i][prop];
    } else if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop) == false) {
      return "No such property";
    }
  }
  if (foundName < 1) {
    return "No such contact"
  };
}

var foundName = 0;

// Only change code above this line


// Change these values to test your function
var ans = lookUpProfile("Bob", "number");
console.log(ans);

So I'm looping through the pre-defined array with my for loop and I'm checking for instances where name == firstName and where the object has a property of prop. In these instances I am returning the property. Otherwise, I return "No such Property". I'm also changing my variable foundName so that when firstName is matched to name in the loop, foundName gets a positive value. IF foundName is less than 1 (i.e. no name matches found) then I return 'No such contact'.

Now, when I run this in my browser and look in the console it seems to work perfectly. However, when I enter this answer into freecodecamp, I get:

"Bob", "number" should return "No such contact" "Bob", "potato" should return "No such contact"

But if I put, for example, "Bob" and "number" into the function, I do get "no such contact"... I must be missing something obvious here but I am extremely confused by this!!

Barmar
  • 741,623
  • 53
  • 500
  • 612
DevB1
  • 1,235
  • 3
  • 17
  • 43
  • You're missing `var contacts = [` before the array at the beginning of the code. – Barmar Jun 01 '18 at 11:43
  • You never initialize `foundName` to `0`. You should just use a boolean `true/false` for this, BTW. – Barmar Jun 01 '18 at 11:44
  • Here's all you need: `var match = contacts.filter(c => c.firstName === name); if (!match.length) return "No such contact"; return match[0][prop] || "No such property";` –  Jun 01 '18 at 11:46
  • Sorry I didn't copy the var contacts = [ over but it was there in my code - I've added it now. And I do initialize foundName to 0. – DevB1 Jun 01 '18 at 11:48
  • @ChrisG That's way better, but maybe too advanced for him, I think he is just starting off with JavaScript ;) – Ivan Jun 01 '18 at 11:48
  • @Ivan yep that's right!! To be fair, I know there will be loads of ways to solve this. What I don't understand is why freecodecamp is rejecting my code when, when I run this in my console, all the required results appear fine – DevB1 Jun 01 '18 at 11:49
  • I can't see the reason, either. – Barmar Jun 01 '18 at 11:50
  • on freecodecamp do you have a sort of editor to test it? – Ivan Jun 01 '18 at 11:50
  • Yes. Actually problem solved now - you were right, simple scope issue :). Cheers – DevB1 Jun 01 '18 at 11:55

2 Answers2

1

The problem is in your variable foundName every time you call the function lookUpProfile you change the global variable foundName so when you call the function ** lookUpProfile** with 'bob' as argument it will return undefined because the value of foundName is greater that 1 , so you need to scope the variable to the function block scope , you should define the varaible foundName inside the function not outside it , also stop using the key word var and instead use let

look at the solution below

//Setup
var contacts = [
    {
        "firstName": "Akira",
        "lastName": "Laine",
        "number": "0543236543",
        "likes": ["Pizza", "Coding", "Brownie Points"]
    },
    {
        "firstName": "Harry",
        "lastName": "Potter",
        "number": "0994372684",
        "likes": ["Hogwarts", "Magic", "Hagrid"]
    },
    {
        "firstName": "Sherlock",
        "lastName": "Holmes",
        "number": "0487345643",
        "likes": ["Intriguing Cases", "Violin"]
    },
    {
        "firstName": "Kristian",
        "lastName": "Vos",
        "number": "unknown",
        "likes": ["JavaScript", "Gaming", "Foxes"]
    }
];

function lookUpProfile(name, prop) {
  // Only change code below this line
  // Only change code below this line
   let foundName = 0;
  for (var i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName == name) {
      foundName += 1;
    }
    if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop)) {
      return contacts[i][prop];
    } else if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop) == false) {
      return "No such property";
    }
  }
  if (foundName < 1) {
    return "No such contact"
  };
}




// Only change code above this line


// Change these values to test your function
// Change these values to test your function
lookUpProfile("Akira", "likes");
mostafa tourad
  • 4,308
  • 1
  • 12
  • 21
0

You object declaration is not correct: contacts is an Array.

The function works but you need to keep foundName enclosed in the function rather than outside. Otherwise, if you run your function twice it will break.

Here is the working code:

const contacts = [{
  "firstName": "Akira",
  "lastName": "Laine",
  "number": "0543236543",
  "likes": ["Pizza", "Coding", "Brownie Points"]
}, {
  "firstName": "Harry",
  "lastName": "Potter",
  "number": "0994372684",
  "likes": ["Hogwarts", "Magic", "Hagrid"]
}, {
  "firstName": "Sherlock",
  "lastName": "Holmes",
  "number": "0487345643",
  "likes": ["Intriguing Cases", "Violin"]
}, {
  "firstName": "Kristian",
  "lastName": "Vos",
  "number": "unknown",
  "likes": ["Javascript", "Gaming", "Foxes"]
}];


function lookUpProfile(name, prop) {

  let foundName = 0;

  for (var i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName == name) {
      foundName += 1;
    }
    if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop)) {
      return contacts[i][prop];
    } else if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop) == false) {
      return "No such property";
    }
  }
  if (foundName < 1) {
    return "No such contact"
  };
}

console.log(lookUpProfile("Kristian", "lastName"))
console.log(lookUpProfile("Bob", "number"))
Ivan
  • 34,531
  • 8
  • 55
  • 100
  • The object declaration problem was just a copying error. – Barmar Jun 01 '18 at 11:50
  • Putting that first made it seem like that was your main point, and the `foundName` issue was less important. – Barmar Jun 01 '18 at 11:55
  • Well it seemed pretty important at the time because the code didn't run. The second only affects the execution when the function is called more than once – Ivan Jun 01 '18 at 11:57
  • Yeah. I upvoted the other answer because he went into more detail about why this is the serious problem. – Barmar Jun 01 '18 at 12:02
  • @Barmar, true, I understand – Ivan Jun 01 '18 at 12:05