Trouble with looping script, returning 1% interest gained

Hi, I am trying to write a script that will return a number incrementing by 1%.

Similar to the question: If I have 1 dollar, and it gains 1% everyday, how long will it take to reach 1 million?

I have tried writing functions and loops, but the only way I can successfully do this is to tediously write out an extremely long script. I know there has to be a better way to do this.

Any hints?

let ragsRiches = (x) => {
  let dayOne = x*1.01;
  let dayTwo = dayOne*1.01;
  let dayThree = dayTwo*1.01;
  let dayFour = dayThree*1.01;
  let dayFive = dayFour*1.01;
  let daySix = dayFive*1.01;
  let daySeven = daySix*1.01;
  console.log("Starting Balance: " + x);
  console.log("Day One Balance: " + dayOne);
  console.log("Day Two Balance: " + dayTwo);
  console.log("Day Three Balance: " + dayThree);
  console.log("Day Four Balance: " + dayFour);
  console.log("Day Five Balance: " + dayFive);
  console.log("Day Six Balance: " + daySix);
  console.log("Day Seven Balance: " + daySeven);
  }

Can you show us a loop that you’ve tried? It can be done with a for loop, but a while loop would probably make more sense.

1 Like

Math is your friend. For each day, you take the previous value and multiply it with the same factor:

Day 1 : x * 1.01
Day 2 : x * 1.01 * 1.01
Day 3 : x * 1.01 * 1.01 * 1.01

And so on.
I’m sure you see a relation between the number of days and the number of times you have to multiply x with 1.01. You’d need JavaScript’s Math.pow() function.

For the opposite way (“how many days does it take until I reach a certain goal”) you’d need Math.log, but it depends on if you’re a math person. You can as well solve it with a loop.

Sure. I suspect I am missing something in the loops, because I can only get it to print the first result, not update the base value and repeat.

let x = 1
while (x < 100) {
  let y = x*1.01
  console.log(y)
}}

Thanks, I will look into Math.pow() and Math.log

You have a few problems here.

First of all, you are not indexing your control variable, so you should have an infinite loop.

The other problem is that you declaring your y variable (I think we can come up with a better name) inside the loop so it is being redeclared on every iteration. Also, why is it “x*1.01”?

1 Like

Ok, so I should be using an array here? Is this what you mean by indexing? So it can be called like variable[1] ? Then I should be looking at looping through an array, using map to update it? Then creating a second function to console.log "Day x Balance: " ?

The problem I had with loops initially was updating the main value, and running the loop against the updated value

Thank you for your time

Ok, so I should be using an array here? Is this what you mean by indexing?

No, I mean x. Maybe “indexing” isn’t the best word here. The issue is that you are using x to control the loop. But x never changes. So the while (x < 100) will always be true so it will run forever. You need x to change, presumably increment.

Unless I’ve misunderstood what x is supposed to be. That’s the problem with one letter variable names. Good code should read like a story - good variable names are an important part of that.

Maybe take a step back…

Can you write a while loop that starts with a variable at 1 and then keeps logging it and incrementing it until it reaches 20?

Yep. This is precisely where I run into an issue. The only amount I can increment the variable by is 1. Instead I want to increment by a percentage.

let beginning = 1

while (beginning < 20) {
  console.log('Thank you for your replies')
  beginning++
}

Nope, you didn’t misunderstand - you understand exactly. I can’t get x to change/update

In my later code example I gave a better variable name. I think you are right about that, duly noted friend. It will probably help with learning as well

you increment by a percentage using multiplication

the issue is that x was never changing and y always got to the same result

can you think of how to change your beginning++ to increment in the way you want?

also, note that beginning is not a descriptive name if it also changes - the name should still be applicable after any change

Right, I agree with what iahleen is saying.

So, you can increment and stop at your goal. Now, can you multiply that number? If you change one line, you can have it do an increase as interest. The next step would be to have another variable to keep track of what iteration you are on, but that’s simple incrementing.

at the end of the loop you should increase x by 1
try this:

let x = 1
while (x < 100) {
  let y = x*1.01
x++
}}  
console.log(y)

that will not work, at the end y will be equal to 99.99, which is not the vaue for 99 times
1% of interest, and also not accessible outside of the loop

Thank you so much, I have figured it out!

let startingBal = 1
while (startingBal < 1000000) {
  let newBal = startingBal * 1.01
  console.log(newBal)
  startingBal = newBal}

This worked! Is this the proper way to complete a task like this, or is there a better way? I am now off to build a form like input where a user can input a starting amount, input their goal amout, by day/week, then hit calculate, and the script will output the numbers along with some text to make it look nice

I would not do it like this. I would use some arithmetic to make this much faster.

You are computing startingBal * 1.01 * 1.01 * 1.01 * ... * 1.01.

Why not use the fact that you are computing startingBal * 1.01**numberOfDays?

Look at this page, especially problem 4

Just want to say thank you so much. Thanks to your efforts I was able to overcome where I was getting stuck.

This is what worked for me. But please let me know if there is a better, more professional way of writing this script, as this is after all a learning exercise for me

let startingBal = 1
while (startingBal < 1000000) {
  let newBal = startingBal * 1.01
  console.log(newBal)
  startingBal = newBal}

My next step is to develop it into a a simple web page, where a user can input starting balance, goal to reach, and percentage gain each day/week, hit calculate, and the script will output on the page the results in a nice way with words and such…“Day 23 Balance: 300 | Percent Interest: x | etc:”

Why not use the fact that…

I think he was looking to work on loops and also wanted a count of iterations.

Is this the proper way to complete a task like this, or is there a better way?

Sure, that works. I’m not sure that you need to reassign startingBal - for one thing it makes that name misleading. I would just call it balence and use the *= operator.

let balence = 1
while (balence < 1000000) {
  balence *= 1.01
  console.log(balence)
}

Then you just need to add in a counting variable if you want to log which iteration it is on.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.