Replace with recursion

Tell us what’s happening:
Hi,

Does anyone tried to replace the for loop of this exercise with recursion?

I tried but I think I’m very lost for do it correctly, even I don’t know if this is possible

I will appreciate if someone can tell me how far Im or if this is possible.

thx

Your code so far


// 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 length = []
function factorial(x){
if(x <= 0){
    return 0;
} else {
length.push(x - 1);
}
return length
}

console.log(factorial(contacts.length));

function lookUpProfile(name, prop){
// Only change code below this line
if(contacts[length].firstName === name) {
if(contacts[length].hasOwnProperty(prop)){
    return contacts[length][prop]
} else {
    return "No such property";
}
}
return "No such contact";
// Only change code above this line
}
console.log(contacts[length].firstName);
console.log(lookUpProfile("Harry", "likes"));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36.

Challenge: Profile Lookup

Link to the challenge:

Yes, it is possible to solve this challenge with recursion. I just tried it, and it worked.

I’m not quite sure what the factorial function is meant to do in your code, but I can describe briefly how I solved it.

The way I approach all recursion problems is to first identify - when the function should terminate and what it should return in that case. Then, you reduce the problem in each recursive call, so that it leads to the base case you identified.

For this particular challenge, if you were to create a recursive function, it should terminate when there is no more “contact” (in the contacts array) left to check. This is the base case. What it returns in such a case would depend on whether you find a matching contact, but with no matching prop, or you didn’t find a matching contact at all. The challenge describes which string to return in either case.

Obviously, during the recursion, if you did find a matching contact with a matching prop, you return immediately.

The solution is fairly simple. Without giving too much away, it helps if you think what you would do in a regular “for loop”, and then try to rewrite that logic in terms of recursive function calls.

Good luck!

Thx Manish,

I really really tried to but I feel more lost that I was when I tried for the first time. I’ll appreciate if you can give me a more detail explanation or help me with an example?

Greeting