Recursion of Objects

Tell us what’s happening:
You can Ignore the code as I have solved it with the normal “for loop” method with nested “ifs” and passed the test. However, the exercise before this taught us recursion and I was curious to know how (if even possible) it would be applied here?

Is recursion exclusive to mathematical issues? It’s more the curiosity in my head that needs to know this as the “functionName(arr, n-1)” and “arr[n-1]” can’t work when the arguments for the function do not have an “n” to begin with, right? I do understand that they are different scenarios altogether but I just need to know if it is possible and if so, how?

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"]

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

// Only change code above this line

lookUpProfile("Akira", "likes");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36.

Challenge: Profile Lookup

Link to the challenge:

Welcome, Suhail.

To be honest, I cannot think of a way to solve this exercise with recursion. However, this is mainly due to the bad practice of involving outside variables within a function.

What I mean is, it might be possible if one of the arguments passed to lookUpProfile was the contacts array.

That depends on what you call a mathematical issue. All JS objects can be indexed into (of sorts), and indices are numbers, and numbers can be operated on in many ways mathematically to return a non-mathematical object.

I will leave you with this:

The reduce method comes to mind because it operates recursively on all array objects.

Hope this helps

1 Like

Yeah I don’t think recursion is the solution in this case. Recursion in my experience is used for traversing nested data structures like XML and Trees. This is simple collection of objects, so a for loop is probably the best bet.

1 Like

And what I actually find more interesting about this problem is it appears to be looking up by first name. So what if you have more than one contact with the same first name? Do you return the first one found? An array of all of them found (which introduces other complications with the second argument) …

function lookUpProfile(name, prop, index = 0) {

  // to check invalid numbers put in
  // and avoid error "impossible to read property ___ of undefined"
  // not really necessary here, just good practice to account for this
  if (index < 0 || typeof index !== "number") index = 0

  if (contacts[index]) {
    if (contacts[index].firstName === name) {
      return contacts[index][prop] || "No such property"
    } else {
      return lookUpProfile(name, prop, index + 1)
  } else {
    return "No such contact"


or something like that, anyway, even if you can solve something with recursion, and you can actually solve many things with it, it doesn’t mean you should

this is ugly, I would just do it in a case like this for exercise

anyway, yes, it’s possible

1 Like

The most common situations where recursion comes up “naturally” are structures that are recursive in nature. IE parts of the problem look the same as the over all problem. For example, recursive solutions would make sense when dealing with tree data structures. You could call data structures “mathematical issues”, but they are very much “programmer math”, in the sense they appear all the time when programming.

On applying recursive solutions outside of these problems I generally wouldn’t force it.

1 Like