Creating a basic JavaScript to add 100, 15 times to equal out to 1500. However, the code down below that I did is not triggering and only returning 0. I'm pretty sure I'm over thinking this or having my variables be called incorrectly.
var monthlyDeposit = 100; // Code will be tested with values: 100 and 130 var accountBalance = 0; /* Your solution goes here */ for (c = accountBalance; c <= 15; c += monthlyDeposit) { console.log(c); }
If you are starting with Javascript
, then let the c
variable handle the iterations and put the logic that increments the accountBalance
inside the loop body. This will make more explicit that the loop executes 15
times.
var monthlyDeposit = 100; // Code will be tested with values: 100 and 130 var accountBalance = 0; /* Your solution goes here */ for (let c = 0; c < 15; c++) { accountBalance += monthlyDeposit; } console.log("accountBalance is: " + accountBalance);
.as-console {background-color:black !important; color:lime;} .as-console-wrapper {max-height:100% !important; top:0;}
In your code, you are adding the value of monthlyDeposit
to c
, So for the first iteration, the value of c
will be 100 which is not satisfying c <= 15
this condition.
var monthlyDeposit = 100; // Code will be tested with values: 100 and 130
var accountBalance = 0;
/* Your solution goes here */
for (c = accountBalance; c <= 15; c += monthlyDeposit) { //value of c = 100
//It will never come inside as c (100) > 15.
console.log(c);
}
var monthlyDeposit = 100; // Code will be tested with values: 100 and 130 var accountBalance = 0; /* Your solution goes here */ for (c = accountBalance; c <= 15; c += monthlyDeposit) { console.log(c); //<- c is 0 after that it will become 100 which is not less than 15 }
Because after the first iteration the value of c
is 100
which is not less than the 15
hence you are getting the value of c
as 0
You are adding 100 to c but the condition you are checking is <= 15. Your loop will run only once.
Make the condition as c <= 1500 and it should work.
Initally c=0
.After first iteration c = 100
which is greater than 15
.If you want to run this 15 times. You set condition c <= 1500
var monthlyDeposit = 100; // Code will be tested with values: 100 and 130 var accountBalance = 0; /* Your solution goes here */ for (c = accountBalance; c <= 1500; c += monthlyDeposit) { console.log(c); }
var monthlyDeposit = 100; // Code will be tested with values: 100 and 130 var accountBalance = 0; /* Your solution goes here */ for (let c=1; c<= 15; c++) { accountBalance+=monthlyDeposit; } console.log("Account Balance:"+accountBalance);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.