Fetch from three APIs, rank by market cap, and merge
You are tasked with creating a function that retrieves information about trending stocks based on their market capitalization and prices. The goal is to identify the top n trending stocks, where n is a specified number, and return details about these stocks in a structured format. The function should return an array of objects, each encapsulating the symbol, name, market capitalization, and current price of these trending stocks. MDN’s Array.prototype.sort() reference covers the comparator you will need to rank trending stocks.
The challenge involves fetching data from three different APIs: one for stock symbols and names, another for market capitalizations, and the last one for current stock prices. The process is to first fetch the symbols and market caps, sort the stocks by market cap in descending order, pick the top n stocks, and then fetch their current prices. Finally, the information is aggregated into a single array of objects that contain all the necessary details for each selected stock.
Consider you want to find the top 2 trending stocks. The function will sort all available stocks based on their market capitalization and return the details for the top 2.
Input: n = 2
Output:
[
{ "symbol": "MMM", "name": "3M Company", "market-cap": 138721055226, "price": 1001.52 },
{ "symbol": "ABT", "name": "Abbott Laboratories", "market-cap": 102121042306, "price": 1001.52 }
]
The function encapsulates a complex process of asynchronous API calls, data sorting, and aggregation, presenting a streamlined solution to the user. This task not only tests your proficiency with asynchronous JavaScript operations but also your ability to work with external data sources and transform data to fit specific requirements.
This challenge serves as an excellent real-world scenario for frontend developers, especially for those interested in financial technologies or stock market applications, providing both a technical challenge and an insight into financial data manipulation.
n as an argument, representing the number of trending stocks to retrieve.symbol, name, market-cap, and price for each trending stock.n stocks are available.