1

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);
}

2
  • 4
    c starts at 0 and gets incremented by 100 each time, so only the first iteration (while c is below 15) runs...? Commented Feb 18, 2019 at 5:10
  • change c <= 15 to c <= 1500 ? Commented Feb 18, 2019 at 5:12

6 Answers 6

2

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;}

Sign up to request clarification or add additional context in comments.

Comments

1

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);
}

Comments

0

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

Comments

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.

Comments

0

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);
}

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.