In: Computer Science
Write a defining table and a JavaScript program that computes and outputs the rate of change of a price. The program must allow the user to enter a previous price and a current price. The formula for the rate of change is
rateOfChange = currPrice - prevPrice
prevPrice
Your program should allow a user to enter real numbers such as 7.54. If you wish, you may use the following HTML code in your program.
<!DOCTYPE HTML> <html lang="en-us"> <head> <meta charset="utf-8"> <title>Rate of Change</title> <script> // Your JavaScript function belongs here. </script> </head> <body> <h1>Rate of Change</h1> Previous Price <input type="text" id="prevPrice"><br> Current Price <input type="text" id="currPrice"><br> <button type="button" onclick="computeRateOfChange()">Rate of Change</button><br> Rate of Change <div id="rate"></div> </body> </html>
Test Cases | ||
---|---|---|
Inputs | Output | |
previous | current | |
70.5 | 72.2 | 0.0241 |
96 | 91.4 | -0.0479 |
<!DOCTYPE HTML>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>Rate of Change</title>
<script>
function computeRateOfChange(){
var previousPrice =
parseInt(document.getElementById("prevPrice").value);
var currentPrice =
parseInt(document.getElementById("currPrice").value);
var rateOfChange = currentPrice-previousPrice;
document.getElementById("rate").innerHTML=rateOfChange;
}
</script>
</head>
<body>
<h1>Rate of Change</h1>
Previous Price <input type="text"
id="prevPrice"><br>
Current Price <input type="text"
id="currPrice"><br>
<button type="button" onclick="computeRateOfChange()">Rate of
Change</button><br>
Rate of Change
<div id="rate"></div>
</body>
</html>
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME