Home > Blockchain >  Looking for help to understand a question for a JavaScript application
Looking for help to understand a question for a JavaScript application

Time:08-29

I need help to understand what exactly this question wants as return. The example of input with the output does not make sense to me. I think page input is what's causing all the confusion.

The interface of a stock app manager receives data of the transactions did on that day in JSON.

Build the fuction 'getPageData', which will be responsible for paging the quantity of actions from its users.

Requirements:

1 - The function must receive a JSON string, the amount of rows that will be returned for pages, and the number of pages that will be returned.

2- The return must have the sum of the user's stocks, adding the stocks of all companies*

3- The return must be ordered in descending of the amount of each user's stocks. It doesn't matter the order if two users have the same stock amount.

4- The return of the function must be a JSON with the amount of stocks, considering the given page and the amount of data for each page. the page count must start with 1.

This is the starting code given

    function getPageData(dayTrade, pageSize, pageNumber) {
    var arrayTrade = [];
    arrayTrade = JSON.parse(dayTrade);

    
    console.log(dayTrade);
}

var dayTrade = `[
    {"user": "Rob", "company": "Google", "countOfStocks": 5},
    {"user": "Bill", "company": "Goldman", "countOfStocks": 18},
    {"user": "Rob", "company": "JPMorgan", "countOfStocks": 10},
    {"user": "Dave", "company": "Boeing", "countOfStocks": 10}
]`;

console.log(getPageData(dayTrade, 1, 2)); // page size = 1, page number = 2

The link is the input with output example [1]: https://i.stack.imgur.com/NlQVD.png

I know it's a big question, but no one around me was able to complete understand this question too.

EDIT: Sorry, I know this question was too vague.

Here we have input

var dayTrade =
`[
{"user": "Rob", "company": "Google", "countOfStocks": 5},
{"user": "Bill", "company": "Goldman", "countOfStocks": 18},
{"user": "Rob", "company": "JPMorgan", "countOfStocks": 10},
{"user": "Dave", "company": "Boeing", "countOfStocks": 10}
]`;
console.log(getPageData(dayTrade, 1, 2));

Where 1 is the page size and 2 is the page number.

And here we have the output

'[{"user": "Rob","totalStocks": 15}]'

I can't understand why this would be the output with page number being 2.

My understand is that each JSON row of the input is a page. Doen't it make "user": "Rob", "company": "Google", "countOfStocks": 5} page 1 and {"user": "Bill", "company": "Goldman", "countOfStocks": 18} page 2?

The input says the page number is 2, so why would it return the sum of Rob's stocks?

CodePudding user response:

The example of input with the output does not make sense to me.

We should imagine each of the distinct users (occurring in the input) to get one line on a page. Each page has a limited number of lines, so there might be the need for more than one page. The confusion starts here:

each JSON row of the input is a page.

No, the example input has four records, but since two of those relate to the same user, there are actually only three users, and so we must imagine putting those three lines on pages.

why would it return the sum of Rob's stocks?

The requirement #2 explicitly states that the sum of stocks must be output.

Doesn't it make {"user": "Rob", "company": "Google", "countOfStocks": 5} page 1 and {"user": "Bill", "company": "Goldman", "countOfStocks": 18} page 2?

No, for several reasons:

  • the stocks for the same user must be summed up first (there's no sum that is 5 in the example)
  • the users should be sorted by descending (sum of) stocks
  • the company is not part of the output (because that information is ignored by summing up stocks per user)

If we stick to the descending order of sum-of-stocks, we can know which of the user(s) will end up on the second page. In this example, that user is Rob, because Rob's stocks amount to a total of 15 (5 10), while Bill has 18, and Dave has only 10. So the order is Bill, Rob, Dave. That's why Rob ends up on the second page.

The return value is a JSON string encoding an array, because in general a page can hold multiple users. That's not the case in the example, because the page size is 1. But for larger page sizes, the output needs to include all records that will appear on the page that is requested.

I hope this explains why the output has to be:

'[{"user": "Rob", "totalStocks": 15}]'

What to do:

To solve this challenge you need to think of these tasks:

  • Aggregation: the input objects need to be aggregated (in a new data structure) so that you end up with just one object per user, without the company information, and with the stocks of the given user summed up.

  • Sorting: the aggregated objects need to be sorted by descending stocks.

  • Paging: the array of aggregated objects needs to be sliced to get the part that would end up on the given page, given the page size. Some simple arithmetic is needed here.

  • JSON I/O: the input must be parsed (code is already given), and the output must be stringified.

CodePudding user response:

This sounds like school homework, but I will give you one answer to one of the questions, you should be able to figure out the rest.

    function getPageData(dayTrade, pageSize, pageNumber) {
    var arrayTrade = [];

    dayTrade.sort((a, b) => a.countOfStocks - b.countOfStocks)
    console.log(dayTrade);
}

var dayTrade = [
    {"user": "Rob", "company": "Google", "countOfStocks": 5},
    {"user": "Bill", "company": "Goldman", "countOfStocks": 18},
    {"user": "Rob", "company": "JPMorgan", "countOfStocks": 10},
    {"user": "Dave", "company": "Boeing", "countOfStocks": 10}
];

console.log(getPageData(dayTrade, 1, 2)); // page size = 1, page number = 2

  • Related