I am struggling to make a For Loop statement in JavaScript that will double .01 365 times (double a penny for a year). Please help!
This doubles the penny but only up to the amount of 365...how do i get it to double 365 times?
var i = .01
for (; i < 365; i *=2){
console.log(i);
}
Thank you for your help!!
I am struggling to make a For Loop statement in JavaScript that will double .01 365 times (double a penny for a year). Please help!
This doubles the penny but only up to the amount of 365...how do i get it to double 365 times?
var i = .01
for (; i < 365; i *=2){
console.log(i);
}
Thank you for your help!!
Share Improve this question asked Jan 23, 2018 at 14:33 CorinaCorina 859 bronze badges 6- you need two variables, one to count from 1 to 365, and one to store a dollar amount – James Commented Jan 23, 2018 at 14:36
- Is this what you want to console? 0.01, 0.02, 0.04, 0.08, 0.06.....etc – Alcaeus D Commented Jan 23, 2018 at 14:36
-
2
0.01 * Math.pow(2, 365)
? – Ovidiu Dolha Commented Jan 23, 2018 at 14:38 - yes, I want the console to show .01, .02, .04, etc 365 times ...how do i set it up with 2 variables? – Corina Commented Jan 23, 2018 at 14:45
-
(double a penny for a year).
Eh??, Solutions so far are doubling a penny every day for a year. Is that what your after.. Are you setting up a bank for savings investments, you can have some of my money at that rate.. :) – Keith Commented Jan 23, 2018 at 14:49
2 Answers
Reset to default 9If you know precisely how many times you need to double the number (365 in this case), then you really don't even need a for loop. You can just multiple by a power of two:
let i = 0.01 * Math.pow(2, 365);
Simple, you are using the same value for counting and multiplying. If you want to finish it off with 1 variable, then you have to put the value you expect to get at the end of 365 loops on the underscored part: for (var i = 0.01; i <= __; i++) {
. But, an easier way to do this is:
var penny = 0.01;
for (var i = 0; i < 365; i++) {
console.log(penny);
penny = penny*2;
}
console.log(penny);