Mentor
Mentor

Reputation: 1003

For loop has different values inside vs outside the loop

I am attempting to make a compound interest calculation with a recession in an arbitrary year. The following promise function calculates the development of the variable portfolio.

EDITS FOR CLARITY:

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 ) )

The kicker is that everything goes perfect, except that the final outcome of the portfolio variable is a value that makes no sense to me. ALL OF THEM return 180.82637675169082.

I can see in the console.log statements that the for loop works perfectly right up until the last run of the loop, where it suddenly decides 180.82637675169082 is the answer. Which is impossible.

Output of the above:

[ { year: 1, result: 180.82637675169082 },
  { year: 2, result: 180.82637675169082 },
  { year: 3, result: 180.82637675169082 },
  { year: 4, result: 180.82637675169082 },
  { year: 5, result: 180.82637675169082 },
  { year: 6, result: 180.82637675169082 },
  { year: 7, result: 180.82637675169082 },
  { year: 8, result: 180.82637675169082 },
  { year: 9, result: 180.82637675169082 },
  { year: 10, result: 180.82637675169082 },
  { year: 11, result: 180.82637675169082 },
  { year: 12, result: 180.82637675169082 },
  { year: 13, result: 180.82637675169082 },
  { year: 14, result: 180.82637675169082 },
  { year: 15, result: 180.82637675169082 },
  { year: 16, result: 180.82637675169082 },
  { year: 17, result: 180.82637675169082 },
  { year: 18, result: 180.82637675169082 },
  { year: 19, result: 180.82637675169082 },
  { year: 20, result: 180.82637675169082 } ]

Looking at the console.log statements it looks like the last run of the loop is what breaks things. The portfolio variable develops fine until the last run, and then it just turns into 180.82637675169082.

Upvotes: 4

Views: 111

Answers (2)

Mentor
Mentor

Reputation: 1003

My assumptions were wrong.

I assumed that in a compound interest calculation the year in which the portfolio halves matters.

In real world terms: I assumed that if you invest X amount of money, it matters whether a recession occurs next year or in 10 years.

Turns out it does not.

PBKAC.

Many thanks to those of you who helped me reach this conclusion.

Upvotes: 1

mostafa tourad
mostafa tourad

Reputation: 4388

If you analyze your code you'll find that you do the same calculations every time so the result will be the same , look at this line of your code

            thisyear == recyear ? ( portfolio *= crash ) : ( portfolio *= grow )

the var thisyear will equal recyear only one time the portfolio will multiply by the crash and this will happen only one time << else the portfolio will multiply by the grow >>

also every variable is the same in every promise except recyear so nothing will change the result except you change other variables

so finally I hope you notice that problem

Upvotes: 0

Related Questions