Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

528
Views
Javascript Books challenge

I tried to complete a javascript assessment for a job interview but failed

My plan was to process the user object inside findPotentialLikes function but it'll always throw undefined.

It is made using Mocha and Chai testing framework and promises.

Basically my question is: how do i get the user in the findPotentialLikes promise ?

class SocialNetworkQueries {

    constructor({ fetchCurrentUser }) {
        this.fetchCurrentUser = fetchCurrentUser;
    }

    findPotentialLikes({ minimalScore } = { }) {
        
     console.log(this.fetchCurentUser);
     // I tried here to get the user in order to process the findPotentialLikes function with no luck

        return Promise.resolve();
        
    }

}


describe('SocialNetworkQueries', () => {
  describe("example from README", () => {

    it("should find potential likes", async () => {
      // given
      const user = {
        id: "mrouk3",
        likes: {
          books: ["Moby Dick", "Ulysses"],
        },
        friends: [{
          id: "YazL",
          likes: {
            books: ["Ulysses", "War and Peace"],
          }
        }],
      };

      // when
      const potentialLikes = await new SocialNetworkQueries({
        fetchCurrentUser: () => Promise.resolve(user),
      }).findPotentialLikes({ minimalScore: 0.3 });

      // then
      expect(potentialLikes).toEqual({
        books: [
          "The Great Gatsby",
          "Don Quixote",
          "War and Peace",
        ],
      });
    });

  });
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Given that the fetchCurrentUser argument is provided by the test as a function which returns a promise which resolves to a user value, you would need to retrieve the resolved value from the promise, either via async/await:

class SocialNetworkQueries {
    constructor({ fetchCurrentUser }) {
        this.fetchCurrentUser = fetchCurrentUser;
    }

    async findPotentialLikes({ minimalScore } = { }) {
        const user = await this.fetchCurrentUser();

        // ...

        return {
            books: [
                "The Great Gatsby",
                "Don Quixote",
                "War and Peace",
            ]
        };
    }
}

or logic in a .then() callback which will execute after the user has resolved:

class SocialNetworkQueries {
    constructor({ fetchCurrentUser }) {
        this.fetchCurrentUser = fetchCurrentUser;
    }

    findPotentialLikes({ minimalScore } = { }) {
        return this.fetchCurrentUser().then(
            user => {
                // ...

                return {
                    books: [
                        "The Great Gatsby",
                        "Don Quixote",
                        "War and Peace",
                    ]
                };
            }
        );
    }
}

Either approach is functionally equivalent and results in findPotentialLikes() returning a Promise which resolves with the asserted value from the test.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!