Freecodecamp: Profile lookup exercise. Unchecked points although the output is correct

Created on 27 Feb 2016  路  18Comments  路  Source: freeCodeCamp/freeCodeCamp

Challenge Profile Lookup has an issue.
User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36.
Please describe how to reproduce this issue, and include links to screenshots if possible.

My code:

//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"]
    }
];

var name = ["Akira","Harry","Sherlock","Kristian"];
var propDefault=["firstName","lastName","number","likes"];
function lookUp(firstName, prop){
  var a=name.indexOf(firstName);
  var b=propDefault.indexOf(prop);
  if(a>-1&&b>-1){
    return contacts[a][prop];
  }
  if(a==-1){
    return "No such contact";
  }
  if(b==-1){
    return "No such property";
  }

}

lookUp("Sherlock", "likes" );

help wanted bug

Most helpful comment

+1 my my code is correct. checked console output but the second condition doesn't pass out

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

// Change these values to test your function
lookUp("Sherlock", "likes");

All 18 comments

Hi, my first three points are unchecked although the output is the same as what is required.

Okay, so this is a weird one. Something about the fact that the variable is called name is causing a problem with our test runner. When you call the function in the editor the name variable is the array it should be, but when it's called by the testrunner the name variable is getting turned into a string, which causes the values returned by indexOf to be much higher than your code is expecting. Changing the variable name to something different like namesList prevents this from happening. Someone will have to dig into our test running code to figure out the cause of this.

ok. Thank you very much. At least my sleepless nights are worth it.

@Itegman
It's setting window.name, that is why it's getting converted to a string.
Setting window.name is actually the correct behavior, because global.name is window.name. So the test runner is working the way the browser would, but the editor is not.

@arakita Although your is technically correct and as you and @ltegman pointed out - there seems to be a test bug (not sure if this is my fault). You were suppose to use loops and object lookups for this challenge, as the previous challenges before this teach those concepts.

Also, you are not suppose to write anything outside of the function.

Should we specify that this challenge should be completed using loops and lookups?

@JoshFisk Thanks for the info. I didn't realize name was a global variable in browsers. I don't care which way we fall on this (making global variables land on window or not) but it would be nice if the behavior of the editor and the behavior of the test runner matched.

So you can find all the reserved variable names like this (for your own browser anyways)

var windowProps = [];
for(var prop in this){
  windowProps.push(prop);
}
windowProps.sort();
console.log(windowProps);

And tell jshint about them like this
http://jshint.com/docs/options/#predef

Not sure where to do this in the repo though.

@AkiraLaine

You were suppose to use loops and object lookups for this challenge, as the previous challenges before this teach those concepts.

Should we specify that this challenge should be completed using loops and lookups?

I would say yes, you should specify if it's required. Here's why...

1) There is (typically) more than one way to complete a challenge. If you're testing for the use of certain code, the user's answer (that may come from knowledge/study outside of FCC) may solve the challenge, yet yield a _wrong_ answer.

2) The fact that the previous challenges taught those concepts is an unfair assumption. I completed the challenges that are just prior to Profile Lookup about 3 months ago. Profile Lookup didn't exist at the time since FCC recently added many new challenges (Profile Lookup being one of them) for the 2,080 hours of coding. After the new challenges were added, I went back and completed all of the new challenges even though they weren't required. For myself, and those users who also went back to complete the extra challenges, it may have been a long time since we completed the challenges just prior to Profile Lookup. It wasn't necessarily a minute ago, or yesterday like your assumption implies. As FCC will possibly be updated in the future, this situation will be repeated.

In conclusion, you should either specify or don't test for it, IMO. That's up to you.

+1 my my code is correct. checked console output but the second condition doesn't pass out

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

// Change these values to test your function
lookUp("Sherlock", "likes");

@jbemis Your code passes just fine for me. You may want to try refreshing the page and resubmitting.

@ltegman I have tried refreshing, but the page still says I haven't passed the second test.

@jbemis Your code also passes for me (assuming the contacts array is still present). Try restarting the browser completely.

You can also try saving your answer in a text editor and then resetting the challenge. Perhaps you accidentally edited part of the lookup object.

@ltegman bingo! thanks for the advice!

i got this issue too, just clear code ,and refresh browser.

I'm trying to clear our some old issues. Shall we change:
A lookUpProfile function that takes firstName and a property (prop) as arguments has been pre-written for you.

to

Using what you've learned about loops and lookups, complete the lookUpProfile function that takes firstName and a property (prop) as arguments.

there is a redefinition of name in the code.line no.39[var name]

var name = ["Akira","Harry","Sherlock","Kristian"];
var propDefault=["firstName","lastName","number","likes"];
function lookUp(firstName, prop){
var a=name.indexOf(firstName);
var b=propDefault.indexOf(prop);
if(a>-1&&b>-1){
return contacts[a][prop];
}
if(a==-1){
return "No such contact";
}
if(b==-1){
return "No such property";
}

}

lookUp("Sherlock", "likes" );

I noticed that this exercise has thrown a lot of errors.
I did as suggested:
just clear code ,and refresh browser. ctrl + f5
still no joy

I copied the solution from the source(after failing to get ANYTHING to work) but even the source solition is not being accepted.
The history shows a change 8JAN. Is it possible that there was a new bug introduced?
here's the code:
https://goo.gl/sO7DXR

the fix:
this
if(contacts[i].firstName === name)

should be

if(contacts[i].firstName === firstName)
here
https://raw.githubusercontent.com/freeCodeCamp/freeCodeCamp/staging/seed/challenges/02-javascript-algorithms-and-data-structures/basic-javascript.json

Was this page helpful?
0 / 5 - 0 ratings