简单的javascript程序与条件stickked

时间:2017-01-09 09:22:50

标签: javascript performance

var i = 1;                                               //simple varibles with value
var value1 = 2700;
var value2 = 2550;

var constant1 = value1;
var constant2 = value2;

while (value2 <= value1) {                              //condition
value1 = parseFloat(value1) + parseFloat(constant1);
value2 = parseFloat(value2) + parseFloat(constant2);
i = parseFloat(i) + 1;
    if (value2 >= value1) {
    break;}
}
document.getElementById('n').value = i;

一个简单的程序,其中循环未运行且输出为空(进入无限循环)

我想运行循环     value1 = parseFloat(value1)+ parseFloat(constant1);     value2 = parseFloat(value2)+ parseFloat(constant2);     i = parseFloat(i)+ 1; 而value2仍然小于值1

1 个答案:

答案 0 :(得分:-1)

您总是向自己添加value1value2,因此它始终为value2 < value1

您的代码转换为

var value1 = 2700;
var value2 = 2550;
var i = 1;

while (true) {
    value1 += value1;
    value2 += value2;
    i++;

    if (false) {
        break;
    }
}

document.getElementById('n').value = i;