// 3.3.js // 2024-08-31 // $Id: 3.3.js 1.1 2024/10/27 09:46:16 s Exp $ // a, age // df, discount factor // p , probability of becoming dead // pv, present value of x // r, interest rate // x, cash flow 'use strict' // function const sum = x => x.reduce((sum, element) => sum + element, 0); // input const r = 0.08; const istart = 90; const iend = 101; const n = iend - istart; const a = [90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101 ]; const x = [0, 10e3, 10e3, 10e3, 10e3, 10e3, 10e3, 10e3, 10e3, 10e3, 10e3, 10e3]; const p = [0.07, 0.08, 0.09, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.07, 0.05, 0.04]; let pp = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]; // calculating for(let i = 1; i < n + 1; i++){ pp[i] = pp[i - 1] - p[i - 1]; } const df = a.map((_, i) => 1 / (1 + r) ** i); const pv = a.map((_, i) => pp[i] * x[i] * df[i]); // output console.log('3.3.js'); console.log('a ', ...a.map(x => x.toFixed(0).padStart(5))); console.log('x ', ...x.map(x => x.toFixed(0).padStart(5))); console.log('p ', ...p.map(x => x.toFixed(3).padStart(5))); console.log('pp', ...pp.map(x => x.toFixed(3))); console.log('df', ...df.map(x => x.toFixed(3))); console.log('pv', ...pv.map(x => x.toFixed(0).padStart(5))); console.log('sum of pv', sum(pv).toFixed(0)); // end // reference // David Flanagan著 『JavaScript 第7版』(オライリー・ジャパン) 2022-09-13. // ... : p.215. // ** : p.80. // => : p.202. // console.log() : p.630. // const : p.57. // map() : p.182. // padStart() : p.39. // reduce() : p.184. // toFixed() : p.51. // use strict : p.134. // end reference