虽然循环不执行或不循环
While loop not executing or not looping?
// require '04_wonky_coins'
// require 'rspec'
//
// # Catsylvanian money is a strange thing: they have a coin for every
// # denomination (including zero!). A wonky change machine in
// # Catsylvania takes any coin of value N and returns 3 new coins,
// # valued at N/2, N/3 and N/4 (rounding down).
// #
// # Write a method `wonky_coins(n)` that returns the number of coins you
// # are left with if you take all non-zero coins and keep feeding them
// # back into the machine until you are left with only zero-value coins.
// #
// # Difficulty: 3/5
//
// describe "#wonky_coins" do
// it "handles a coin of value 1" do
// wonky_coins(1).should == 3
// end
//
// it "handles a coin of value 5" do
// wonky_coins(5).should == 11
// # 11
// # => [2, 1, 1]
// # => [[1, 0, 0], [0, 0, 0], [0, 0, 0]]
// # => [[[0, 0, 0], 0, 0], [0, 0, 0], [0, 0, 0]]
// end
//
// it "handles a coin of value 6" do
// wonky_coins(6).should == 15
// end
//
// it "handles being given the zero coin" do
// wonky_coins(0).should == 1
// end
// end
function check_coins(hand){
for(var i=0; i<hand.length; i++){
var coin = hand[i]
if(coin !==0){
return i;
} else {
return null;
}
}
return false;
}
function wonkyCoins(n){
var hand = [];
hand.push(Math.floor(n/2));
hand.push(Math.floor(n/3));
hand.push(Math.floor(n/4));
while(check_coins(hand){
var indx = check_coins(hand);
var value = hand[indx];
var index1 = hand.indexOf(hand[indx]);
if (index1 > -1) {
hand.splice(index1, 1);
}
hand.push(Math.floor(value/2));
hand.push(Math.floor(value/3));
hand.push(Math.floor(value/4));
}
return hand.length;
}
程序正在运行,但由于某些原因,while 循环未循环。我怀疑病情有问题。我不确定javaScript是否接受这种条件。然而,在红宝石中它奏效了。有人可以为我解释为什么不工作吗?
您错过了一个括号来关闭 while 循环条件参数。你的代码应该真的是:
while(check_coins(hand)){
...
}
如果这不起作用,那可能是条件本身。尝试:
while(check_coins(hand) !== null){
...
}
while
语句应该像这样修复:
while (check_coins(hand)) {
似乎check_coins
功能只是检查第一个硬币,应该更改为检查所有硬币。 它还返回整数、布尔值或 null 值中的任何一个,具体取决于条件 - 它应该只返回一个整数(第一个非零硬币的索引(或 null(没有非零硬币(。
固定代码如下所示:
function check_coins(hand){
for (var i=0; i < hand.length; i++) {
if (hand[i] > 0) {
return i;
}
}
return null;
}
function wonkyCoins(n) {
var hand = [];
hand.push(Math.floor(n/2));
hand.push(Math.floor(n/3));
hand.push(Math.floor(n/4));
while ((indx = check_coins(hand)) != null) {
var value = hand[indx];
var index1 = hand.indexOf(hand[indx]);
if (index1 > -1) {
hand.splice(index1, 1);
}
hand.push(Math.floor(value/2));
hand.push(Math.floor(value/3));
hand.push(Math.floor(value/4));
}
return hand.length;
}
代码已被清理,使其更具可读性和惯用的Javascript。 然而,它还没有经过测试。
相关文章:
- 如何在Javascript中延迟for循环执行
- 在多个(html)元素上循环执行相同的JavaScript函数属性更改
- Javascript ajax循环执行
- Jquery in 循环执行一些记录计算
- 对于大数据,循环执行速度慢
- 计算循环执行的次数
- 为javascript循环执行实现暂停和恢复机制
- 在url上循环执行同样的操作
- done() 究竟是如何工作的,以及如何在 done() 中循环执行
- 如何停止for循环执行,直到for循环内部的DWR方法执行完成
- JavaScript Bookmarklet:无限循环执行一个函数,然后在页面加载时再次执行该函数
- JavaScript.带有innerHTML的循环在循环执行期间不会更新
- 使用jquery循环执行ajax回调
- Javascript /jquery每次迭代后循环执行函数
- 在JavaScript中设置循环执行的延迟
- 循环执行jQuery语句
- 取消jsfiddle中的无限循环执行
- jQuery在ajax成功后继续循环执行
- 按键排序数组或:为什么我的for循环执行无序
- 通过forEach循环执行任务以复制具有数组长度的任务