-
I am seeking to understand why my code doesn't work, please explain the logic behind my algorithm being flawed.
-
why on the solution code the nested-if statement knows to continue adding from the loss on day 7 on without specifing to add the investment amount.
You put 1000 dollars into a new cryptocurrency. Good luck!
Over ten years, the value of the investment increases by 5% each year.
In the seventh year, the investment loses 75% instead of increasing. Eeek!
Use a for loop to log how many years it has been and how much the investment is worth for each year.
let investment = 1000;
/// MY SOLUTION ///
const interest = .05;
let balance = investment + (investment * interest);
for (i = 1; i < 11; i++){
if (i === 7){
balance = balance * -.75;
} else if (i < 7 || i > 7) {
balance = balance * i;
}
console.log("Year " + i + " investment amount $" + balance);
}
/// SOLUTION ///
for (i = 1; i < 11; i++){
if (i === 7){
investment = investment * .25;
} else {
investment = investment * 1.05;
}
console.log("It has been " + i + " years and we have $" + investment + " left");
}
Aucun commentaire:
Enregistrer un commentaire