我做错了什么?函数进入无限循环,但不应进入无限循环,它应继续减法。我目前最麻烦的是为什么它不减去。
// Create an object which hold the denominations and their values
while(i<obj1.containers.length || obj1.kusur !== 0){
var ttt=Math.floor(obj1.kusur/obj1.containers[i]);
if(obj1.kusur>obj1.containers[i]){
for(var j=0;j<ttt;j++){
ind = obj1.initial.indexOf(obj1.containers[i]);
denomination = obj1.banknote[ind];
obj1.kusur -= obj1.containers[i];
temp += obj1.containers[i];
obj1.kusur= parseFloat(obj1.kusur.toFixed(2));
temp = parseFloat(temp.toFixed(2));
console.log(obj1.kusur+" => "+temp+" => "+obj1.containers[i]+" => "+i);
}
//console.log(i);
if(i===obj1.containers.length || obj1.kusur === 0){
break;
}
i++;
}
/*if(obj1.kusur==temp || obj1.kusur<obj1.containers[i]){
arr1[0] = denomination;
arr1[1] = temp;
change.status = "OPEN";
change.change.push(arr1);
}*/
}
}
这里是pen。结果在控制台中。它在结尾处停止减去。为什么它从一开始就起作用,而在末尾不起作用?
有效的已编辑代码:
while(i<obj1.containers.length || obj1.kusur !== 0){
var ttt=Math.floor(obj1.kusur/obj1.containers[i]);
if(obj1.kusur>obj1.containers[i]){
for(var j=0;j<ttt;j++){
ind = obj1.initial.indexOf(obj1.containers[i]);
denomination = obj1.banknote[ind];
obj1.kusur -= obj1.containers[i];
temp += obj1.containers[i];
obj1.kusur= parseFloat(obj1.kusur.toFixed(2));
temp = parseFloat(temp.toFixed(2));
console.log(obj1.kusur+" => "+temp+" => "+obj1.containers[i]+" => "+i);
}
}
else{
i++;
}
}
答案 0 :(得分:1)
问题是它在$ .05而不是$ .01的容器处获得堆栈。您的i ++位于while循环内的for循环内。基本上,i ++不会被触发,因此它会继续在相同的i值(i = 6)上循环。