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);
}
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.
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”?
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
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.
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
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.
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
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
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:”
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.