-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
53 lines (46 loc) · 1.52 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
document
.getElementById("stockForm")
.addEventListener("submit", async function (event) {
event.preventDefault();
const ticker = document.getElementById("ticker").value.trim();
const apiKey = "";
const resultDiv = document.getElementById("result");
if (!ticker) {
resultDiv.innerHTML = "Please enter a valid stock ticker.";
return;
}
resultDiv.innerHTML = "Calculating...";
try {
const response = await fetch(
`https://financialmodelingprep.com/api/v4/advanced_discounted_cash_flow?symbol=${ticker}&apikey=${apiKey}`
);
if (!response.ok) {
throw new Error(`Failed to fetch data: ${response.statusText}`);
}
const data = await response.json();
console.log(data);
if (
!data ||
data.length === 0 ||
!data[0].enterpriseValue ||
!data[0].price
) {
throw new Error("Invalid data received from API.");
}
const targetPrice =
(data[0].enterpriseValue + data[0].totalCash - data[0].totalDebt) /
data[0].dilutedSharesOutstanding;
const stockPrice = data[0].price;
resultDiv.innerHTML = `
<p><strong>Stock Target Pricpe for ${ticker.toUpperCase()}:</strong> $${targetPrice.toFixed(
2
)}</p>
<p><strong>Current Stock Price for ${ticker.toUpperCase()}:</strong> $${stockPrice.toFixed(
2
)}</p>
`;
} catch (error) {
console.error(error);
resultDiv.innerHTML = `Error: ${error.message}`;
}
});