While working on a Javascript project, I encountered an issue where I needed to calculate the "Price/Earnings Ratio" from a finance API. However, the API did not have a direct method to retrieve the required value.
The formula for computing the Price/Earnings Ratio for a specific year is as follows:
Last Stock Price of Mentioned Date / Date Earnings Per Share
The problem arose when I had the following data:
let as-of-date = "2019-12-31";
let as-of-date-eps = 2.00;
let ratio-as-of-date = as-of-date-eps/?
Unfortunately, the array containing the nearest stock prices for the mentioned date did not include the specific date "2019-12-31". To accurately compute the Price/Earnings Ratio, I needed to find the index of the nearest stock price entry before that date. In this case, it was the second index or stock-price-history[1].last, resulting in an expected Price/Earnings Ratio of 4.095.
My question now is how can I write the code to ensure that I obtain the correct result?