Not Passing Test Due to console.log()

Hey everyone,

I’m new to coding and have been having so much fun working through he Javascript lessons. This morning I was working on the project “Profile Lookup” under Basic Javascript. My code was outputting the correct answers outlined in the project but when I’d run the test it wouldn’t pass. After an hour I finally figured out that the code would pass if I removed my console.log() functions. I like to use the console.log() function to check my progress while working on each lesson. Can anyone explain to me why having this function throughout my code would keep a test from passing?

I apologize if this has been answered already. I did some searching on the forum but couldn’t find someone else asking the same question. If there has already been a response I’d be happy to just be pointed toward the old discussion.

All the best,
Beadle

it would be better if u gave us a code and a link to where this phenome is occuring

Sure thing!

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

function lookUpProfile(name, prop){
for (var i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName === name) {
        if (contacts[i].hasOwnProperty(prop)) {
            return console.log(contacts[i][prop]);
        } else {
            return console.log("No such property");
        }
    }
}
return console.log("No such contact");

}

Do you need the code for the whole project copied here or just the code that I wrote for the solution?

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

1 Like

Console.log does not return what you think it’s returning. It will output the value to the console, but the actual return value it gives is undefined.

Thank you @stressstressstress, that is very helpful!!!

@chris-tse, would this be an accurate statement?

 return console.log("No such contact");

technically returns “console.log(“No such contact”);” as opposed to returning “No such contact” only which is what the code editor is actually testing for?

1 Like

return console.log(something);
is not the same as
return something;

The first is returning the console.log() function itself. The second is returning the value of something which is what you want to do.

It’s good to use console.log() for debugging. Just put it before the return statement.

console.log("No such property");
return "No such property";

Another way to use console.log() is to log the function calls at the bottom of the code to test what the function is returning for different parameters.

console.log(lookUpProfile("Akira", "likes"));  //should log ["Pizza", "Coding", "Brownie Points"]
1 Like

Thanks @stressstressstress, that was articulated perfectly and validates what I was thinking. Those are great ideas for using console.log() to debug without it affecting the actual return value.

1 Like

Not quite. The code on the right is evaluated first, so the return value of the console.log method is what is returned by your function. Because console.log doesn’t return a value, you return undefined. This is similar to how you can do return someStr.toUpperCase() and what you are actually returning is the result of the toUpperCase method.

2 Likes

Thanks @ArielLeslie, this explains why it returns undefined. Very helpful!

I’m glad I could help. Happy coding!