Skip to content

Instantly share code, notes, and snippets.

@adamgarcia4
Last active April 18, 2023 22:07
Show Gist options
  • Save adamgarcia4/86bcea34b61d5b89ce15850d175f456e to your computer and use it in GitHub Desktop.
Save adamgarcia4/86bcea34b61d5b89ce15850d175f456e to your computer and use it in GitHub Desktop.
const { restClient } = require("@polygon.io/client-js");
const rest = restClient("rt65BvAyd7yr219qQ7M9pHIe7qosYdwl");
const getOpenPrice = async (ticker) => {
if (ticker === 'GOOG') {
return 1000
}
if (ticker === 'AAPL') {
return 2000
}
throw new Error('Ticker not found')
}
const getOpenPriceFromAPI = async (ticker) => {
try {
const result = await rest.stocks.dailyOpenClose(ticker, "2023-01-09");
console.log(result);
return result.open;
} catch (error) {
console.error(error)
return 0
}
}
const getExpensiveStockFinal = async () => {
console.log("============ START ===========")
// Problem Decomposition
// 1. Define Names of Stocks to pull
const ticker1 = ' GOOG '
const formattedTicker1 = ticker1.trim()
const ticker2 = ' AAPL '
const formattedTicker2 = ticker2.trim()
// 2. Pull the stock information from the API
// const ticker1OpenPrice = await getOpenPrice(formattedTicker1)
// const ticker2OpenPrice = await getOpenPrice(formattedTicker2)
const ticker1OpenPrice = await getOpenPriceFromAPI(formattedTicker1)
const ticker2OpenPrice = await getOpenPriceFromAPI(formattedTicker2)
// 3. Display Retrieved Prices
console.log(`The open price of ${formattedTicker1} is ${ticker1OpenPrice}`)
console.log(`The open price of ${formattedTicker2} is ${ticker2OpenPrice}`)
// 4. Compare Stocks and Output Result
if (ticker1OpenPrice > ticker2OpenPrice) {
console.log(`${formattedTicker1} is more expensive than ${formattedTicker2}`)
} else if (ticker1OpenPrice < ticker2OpenPrice) {
console.log(`${formattedTicker2} is more expensive than ${formattedTicker1}`)
} else {
console.log(`${formattedTicker1} and ${formattedTicker2} are the same price`)
}
console.log("============ END ===========")
}
getExpensiveStockFinal()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment