1

I'm learning TDD and I would like to create a function to expected this result.

//What I expect

[
    {book_id: 1, name:'NameBook', average: x},
    {book_id: 2, name:'NameBook', average: x},
    {book_id: 3, name:'NameBook', average: x},
    {book_id: 4, name:'NameBook', average: x},
    {book_id: 5, name:'NameBook', average: x},
    {book_id: 6, name:'NameBook', average: x},
    {book_id: 7, name:'NameBook', average: x},
]

I got to merge two objects with the filter function but I'm having difficulties calculating the average and sum of these values where the id's are equal.

I'm posting here what I got to do https://jsfiddle.net/thiagolmoraes/5xsk7Lgw/1/

Anyone can help me.

    const getBooks = () => {
        return [
            { id: 1, name: 'Python Data Science' },
            { id: 2, name: 'Python Machine Learning' },
            { id: 3, name: 'Development Flask' },
            { id: 4, name: 'Mongo Database' },
            { id: 5, name: 'ULM for Dummies' },
            { id: 6, name: 'Java for Dummies' },
            { id: 7, name: 'Learn Rust in 2 hours' },
        ]
    }


 const getRating = () => {
        return [
            { book_id: 1, client_id: 1, rating: 4.5 },
            { book_id: 1, client_id: 4, rating: 5 },
            { book_id: 1, client_id: 25, rating: 5 },
            { book_id: 1, client_id: 2112, rating: 4 },
    
            { book_id: 2, client_id: 34, rating: 3 },
            { book_id: 2, client_id: 123, rating: 4 },
            { book_id: 2, client_id: 23, rating: 4 },
            { book_id: 2, client_id: 255, rating: 4 },
    
            { book_id: 3, client_id: 98, rating: 2 },
            { book_id: 3, client_id: 45, rating: 1 },
            { book_id: 3, client_id: 223, rating: 3 },
            { book_id: 3, client_id: 213, rating: 1.5 },
    
            { book_id: 4, client_id: 652, rating: 4.5 },
            { book_id: 4, client_id: 42, rating: 4.5 },
            { book_id: 4, client_id: 562, rating: 4.5 },
    
            { book_id: 5, client_id: 2, rating: 5 },
            { book_id: 5, client_id: 2, rating: 5 },
            { book_id: 5, client_id: 2, rating: 5 },
    
            { book_id: 6, client_id: 2, rating: 4.5 },
            { book_id: 6, client_id: 2, rating: 4.5 },
            { book_id: 6, client_id: 2, rating: 4.5 },
    
            { book_id: 7, client_id: 2, rating: 4.5 },
            { book_id: 7, client_id: 2, rating: 4.5 },
            { book_id: 7, client_id: 2, rating: 4.5 },
            { book_id: 7, client_id: 2, rating: 4.5 },
            { book_id: 7, client_id: 2, rating: 4.5 },
        ]
    }
    

const getBooksWithRatings = () => {
    return getBooks().map((book) => {
        book.ratings = getRating().filter((r) => r.book_id === book.id);
        return book;
    });

};
    
console.log(getBooksWithRatings())

2
  • Seems like you made a good attempt over in the JS Fiddle! Would you mind copying it over into your question here so it's easier for people to see? Commented Dec 28, 2020 at 4:28
  • Sorry, I will update Commented Dec 28, 2020 at 4:29

2 Answers 2

2

It seems like you were almost there. You just need to calculate the sum of the ratings array and then divide by its length. This should complete the task for you.

const getBooks = () => {
  return [
    { id: 1, name: "Python Data Science" },
    { id: 2, name: "Python Machine Learning" },
    { id: 3, name: "Development Flask" },
    { id: 4, name: "Mongo Database" },
    { id: 5, name: "ULM for Dummies" },
    { id: 6, name: "Java for Dummies" },
    { id: 7, name: "Learn Rust in 2 hours" },
  ];
};

const getRating = () => {
  return [
    { book_id: 1, client_id: 1, rating: 4.5 },
    { book_id: 1, client_id: 4, rating: 5 },
    { book_id: 1, client_id: 25, rating: 5 },
    { book_id: 1, client_id: 2112, rating: 4 },

    { book_id: 2, client_id: 34, rating: 3 },
    { book_id: 2, client_id: 123, rating: 4 },
    { book_id: 2, client_id: 23, rating: 4 },
    { book_id: 2, client_id: 255, rating: 4 },

    { book_id: 3, client_id: 98, rating: 2 },
    { book_id: 3, client_id: 45, rating: 1 },
    { book_id: 3, client_id: 223, rating: 3 },
    { book_id: 3, client_id: 213, rating: 1.5 },

    { book_id: 4, client_id: 652, rating: 4.5 },
    { book_id: 4, client_id: 42, rating: 4.5 },
    { book_id: 4, client_id: 562, rating: 4.5 },

    { book_id: 5, client_id: 2, rating: 5 },
    { book_id: 5, client_id: 2, rating: 5 },
    { book_id: 5, client_id: 2, rating: 5 },

    { book_id: 6, client_id: 2, rating: 4.5 },
    { book_id: 6, client_id: 2, rating: 4.5 },
    { book_id: 6, client_id: 2, rating: 4.5 },

    { book_id: 7, client_id: 2, rating: 4.5 },
    { book_id: 7, client_id: 2, rating: 4.5 },
    { book_id: 7, client_id: 2, rating: 4.5 },
    { book_id: 7, client_id: 2, rating: 4.5 },
    { book_id: 7, client_id: 2, rating: 4.5 },
  ];
};

const getBooksWithRatings = () => {
  return getBooks().map((book) => {
    const ratings = getRating().filter((r) => r.book_id === book.id);
    book.average = ratings.reduce((acc, el) => acc + el.rating, 0) / ratings.length;
    return book;
  });
};

console.log(getBooksWithRatings());

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Nick, Could you explain me about this reduce?
Sure, the reduce method takes a function with up to four arguments: the accumulator, the current element, the index of the current element, and the original array. I'm only using the first two arguments here. The 0 is the initial value for our accumulator. reduce iterates through the array and adds e.rating to the accumulator, coming up with a new accumulator each time. Essentially, this sums el.rating over the entire array.
1

You can use Array#reduce to find the sum of the filtered array and divide by its length to find the average. To prevent modifying the objects in the original array, you can use spread syntax.

const getBooks = () => {
    return [
        { id: 1, name: 'Python Data Science' },
        { id: 2, name: 'Python Machine Learning' },
        { id: 3, name: 'Development Flask' },
        { id: 4, name: 'Mongo Database' },
        { id: 5, name: 'ULM for Dummies' },
        { id: 6, name: 'Java for Dummies' },
        { id: 7, name: 'Learn Rust in 2 hours' },
    ]
}

const getRating = () => {
    return [
        { book_id: 1, client_id: 1, rating: 4.5 },
        { book_id: 1, client_id: 4, rating: 5 },
        { book_id: 1, client_id: 25, rating: 5 },
        { book_id: 1, client_id: 2112, rating: 4 },

        { book_id: 2, client_id: 34, rating: 3 },
        { book_id: 2, client_id: 123, rating: 4 },
        { book_id: 2, client_id: 23, rating: 4 },
        { book_id: 2, client_id: 255, rating: 4 },

        { book_id: 3, client_id: 98, rating: 2 },
        { book_id: 3, client_id: 45, rating: 1 },
        { book_id: 3, client_id: 223, rating: 3 },
        { book_id: 3, client_id: 213, rating: 1.5 },

        { book_id: 4, client_id: 652, rating: 4.5 },
        { book_id: 4, client_id: 42, rating: 4.5 },
        { book_id: 4, client_id: 562, rating: 4.5 },

        { book_id: 5, client_id: 2, rating: 5 },
        { book_id: 5, client_id: 2, rating: 5 },
        { book_id: 5, client_id: 2, rating: 5 },

        { book_id: 6, client_id: 2, rating: 4.5 },
        { book_id: 6, client_id: 2, rating: 4.5 },
        { book_id: 6, client_id: 2, rating: 4.5 },

        { book_id: 7, client_id: 2, rating: 4.5 },
        { book_id: 7, client_id: 2, rating: 4.5 },
        { book_id: 7, client_id: 2, rating: 4.5 },
        { book_id: 7, client_id: 2, rating: 4.5 },
        { book_id: 7, client_id: 2, rating: 4.5 },
    ]
}


const getBooksWithRatings = () => {
    return getBooks().map(book => {
        const ratings = getRating().filter((r) => r.book_id === book.id);
        return {...book, average: ratings.reduce((acc,{rating})=>acc+rating, 0) / ratings.length};
    });

};

console.log(getBooksWithRatings())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.