基本的For Loop Javascript不触发

时间:2019-02-18 05:08:50

标签: javascript for-loop

创建一个基本的JavaScript以添加100,即15次,等于1500。但是,低于我的代码并不会触发,而只会返回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);
}

6 个答案:

答案 0 :(得分:2)

如果从Javascript开始,请让c变量处理迭代,并执行逻辑以在循环体内递增accountBalance。这将使循环执行15次更加明确。

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

答案 1 :(得分:1)

在您的代码中,您将monthlyDeposit的值添加到c,因此对于第一次迭代,c的值将为100,这不满足{{1} }。

c <= 15

答案 2 :(得分: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
}

由于第一次迭代后c的值为100,该值不小于15,因此您将c的值设为0

答案 3 :(得分:0)

您要在c上加上100,但要检查的条件是<=15。您的循环只会运行一次。

将条件设为c <= 1500,它应该可以工作。

答案 4 :(得分:0)

最初为c=0。第一次迭代c = 100后,它大于15。如果要运行15次。您设置了条件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);
}

答案 5 :(得分: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);