For循环内部与循环外部具有不同的值
我试图在任意一年的经济衰退中进行复利计算。以下promise函数计算变量投资组合的开发。
编辑清晰度:
- const
recessions
是一个数组,循环从1到20(recseverity
是静态的) - 三元(
thisyear == recyear
)在每年不同的年份触发,导致portfolio
在不同年份减少50%
const chunk = ( horizon, principal, roi, recyear, recseverity ) => {
return new Promise( resolve => {
// Decimalise and create portfolio holder
let portfolio = principal
let crash = 1 - ( recseverity / 100 )
let grow = 1 + ( roi / 100 )
// Loop over the years and crash or grow
for (let thisyear = 1; thisyear < horizon +1; thisyear++) {
thisyear == recyear ? ( portfolio *= crash ) : ( portfolio *= grow )
console.log( portfolio )
}
console.log( 'last', portfolio )
// Resolve with the outcome
resolve( { year: recyear, result: portfolio } )
} )
}
const horizon = 20
const principal = 100
const roi = 7
const recseverity = 50
const yearlyadd = principal/horizon
const recessions = []
for (let year = 1; year < horizon +1; year++) {
recessions.push( { year: year, severity: recseverity } )
}
Promise.all( recessions.map( recession => chunk( horizon, principal, roi, recession.year, recession.severity ) ) )
.then( console.log.bind( console ) )
回答如下:
我的假设是错误的。
我假设在复利计算中,投资组合减半的年份很重要。
在现实世界中:我认为如果你投资X金额,那么明年或10年是否会出现衰退就很重要。
事实证明它没有。
PBKAC。
非常感谢那些帮助我得出这个结论的人。
For循环内部与循环外部具有不同的值
我试图在任意一年的经济衰退中进行复利计算。以下promise函数计算变量投资组合的开发。
编辑清晰度:
- const
recessions
是一个数组,循环从1到20(recseverity
是静态的) - 三元(
thisyear == recyear
)在每年不同的年份触发,导致portfolio
在不同年份减少50%
const chunk = ( horizon, principal, roi, recyear, recseverity ) => {
return new Promise( resolve => {
// Decimalise and create portfolio holder
let portfolio = principal
let crash = 1 - ( recseverity / 100 )
let grow = 1 + ( roi / 100 )
// Loop over the years and crash or grow
for (let thisyear = 1; thisyear < horizon +1; thisyear++) {
thisyear == recyear ? ( portfolio *= crash ) : ( portfolio *= grow )
console.log( portfolio )
}
console.log( 'last', portfolio )
// Resolve with the outcome
resolve( { year: recyear, result: portfolio } )
} )
}
const horizon = 20
const principal = 100
const roi = 7
const recseverity = 50
const yearlyadd = principal/horizon
const recessions = []
for (let year = 1; year < horizon +1; year++) {
recessions.push( { year: year, severity: recseverity } )
}
Promise.all( recessions.map( recession => chunk( horizon, principal, roi, recession.year, recession.severity ) ) )
.then( console.log.bind( console ) )
回答如下:
我的假设是错误的。
我假设在复利计算中,投资组合减半的年份很重要。
在现实世界中:我认为如果你投资X金额,那么明年或10年是否会出现衰退就很重要。
事实证明它没有。
PBKAC。
非常感谢那些帮助我得出这个结论的人。
与本文相关的文章
评论列表 (0)
- 暂无评论