第17週 JavaScript 金融計算
吳東泰2021年1月期末考
現值 | |||
利率 | |||
期數 | |||
金流 | |||
終值 |
<h1>吳東泰2021年1月期末考</h1>
<table border="1">
<tbody>
<tr><td>現值</td><td><input id="p" type="number" /></td><td><input onclick="CompP()" type="button" value="計算現值" /></td><td id="p_" width="180"></td></tr>
<tr><td>利率</td><td><input id="y" type="number" value="0.001" /></td></tr>
<tr><td>期數</td><td><input id="n" type="number" value="240" /></td><td><input onclick="CompN()" type="button" value="計算期數" /></td><td id="n_" width="100"></td></tr>
<tr><td>金流</td><td><input id="m" type="number" value="0"/></td><td><input onclick="CompM()" type="button" value="計算金流" /></td><td id="m_"></td></tr>
<tr><td>終值</td><td><input id="f" type="number" value="0"/></td></tr>
</tbody></table>
<script>
function CompP(){ //計算現值 p
var y = document.getElementById("y"). value;
var n = document.getElementById("n"). value;
var m = document.getElementById("m"). value;
var f = document.getElementById("f"). value;
var pvif = y;
pvif++;
pvif =1/pvif**n;
var p = (1-pvif)*m/y + f * pvif;
document.getElementById("p_"). innerHTML = p;
}
function CompN(){ //計算期數 n
var y = document.getElementById("y"). value; //從html表格取得 id=y
var p = document.getElementById("p"). value;
var m = document.getElementById("m"). value;
var f = document.getElementById("f"). value;
var pvif = y;
pvif++;
var n = Math.log( (f - m/y )/( p - m/y ) ) / Math.log(pvif);
document.getElementById("n_"). innerHTML = n;
}
function CompM(){ //計算金流 m
var y = document.getElementById("y"). value;
var n = document.getElementById("n"). value;
var p = document.getElementById("p"). value;
var f = document.getElementById("f"). value;
var pvif = y; //pvif = 利率
pvif++; //pvif = 利率 + 1
pvif =1/pvif**n; //pvif = 1/(利率+1)^n
var m = y * ( p - f * pvif )/( 1 - pvif);
document.getElementById("m_"). innerHTML = m;
}
</script>
<table border="1">
<tbody>
<tr><td>現值</td><td><input id="p" type="number" /></td><td><input onclick="CompP()" type="button" value="計算現值" /></td><td id="p_" width="180"></td></tr>
<tr><td>利率</td><td><input id="y" type="number" value="0.001" /></td></tr>
<tr><td>期數</td><td><input id="n" type="number" value="240" /></td><td><input onclick="CompN()" type="button" value="計算期數" /></td><td id="n_" width="100"></td></tr>
<tr><td>金流</td><td><input id="m" type="number" value="0"/></td><td><input onclick="CompM()" type="button" value="計算金流" /></td><td id="m_"></td></tr>
<tr><td>終值</td><td><input id="f" type="number" value="0"/></td></tr>
</tbody></table>
<script>
function CompP(){ //計算現值 p
var y = document.getElementById("y").
var n = document.getElementById("n").
var m = document.getElementById("m").
var f = document.getElementById("f").
var pvif = y;
pvif++;
pvif =1/pvif**n;
var p = (1-pvif)*m/y + f * pvif;
document.getElementById("p_").
}
function CompN(){ //計算期數 n
var y = document.getElementById("y").
var p = document.getElementById("p").
var m = document.getElementById("m").
var f = document.getElementById("f").
var pvif = y;
pvif++;
var n = Math.log( (f - m/y )/( p - m/y ) ) / Math.log(pvif);
document.getElementById("n_").
}
function CompM(){ //計算金流 m
var y = document.getElementById("y").
var n = document.getElementById("n").
var p = document.getElementById("p").
var f = document.getElementById("f").
var pvif = y; //pvif = 利率
pvif++; //pvif = 利率 + 1
pvif =1/pvif**n; //pvif = 1/(利率+1)^n
var m = y * ( p - f * pvif )/( 1 - pvif);
document.getElementById("m_").
}
</script>
留言
張貼留言