Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当循环不执行或不循环时? //需要'04_摇摇晃晃的硬币' //需要“rspec” // //#卡特西尔瓦式的货币是一件奇怪的事情:他们每个人都有一枚硬币 //#面额(包括零!)。银行里一台摇摇晃晃的兑换机 //#Catsylvania接受任何价值为N的硬币并返回3枚新硬币, //#取值为N/2、N/3和N/4(四舍五入)。 // # //#编写一个方法` wonky_coins(n)`返回您需要的硬币数量 //#如果你拿走所有非零硬币并继续给它们喂食,它们就剩下了 //#回到机器里,直到你只剩下零值硬币。 // # //#难度:3/5 // //描述“#不稳定的硬币”做什么 //它“处理一枚价值1”的硬币 //摇摇晃晃的硬币(1)。应该=3 //结束 // //它“处理价值5”的硬币 //摇摇晃晃的硬币(5)。应该=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]] //结束 // //它“处理一枚价值6”的硬币 //不稳定的硬币(6)。应该=15 //结束 // //它“处理被给予零硬币”的事情 //摇摇晃晃的硬币(0)。应该==1 //结束 //结束 功能检查(手动){ 对于(变量i=0;i-1){ 手工拼接(index1,1); } 手推(数学地板(值/2)); 手推(数学地板(值/3)); 手推(数学地板(值/4)); } 回手长度; }_Javascript - Fatal编程技术网

Javascript 当循环不执行或不循环时? //需要'04_摇摇晃晃的硬币' //需要“rspec” // //#卡特西尔瓦式的货币是一件奇怪的事情:他们每个人都有一枚硬币 //#面额(包括零!)。银行里一台摇摇晃晃的兑换机 //#Catsylvania接受任何价值为N的硬币并返回3枚新硬币, //#取值为N/2、N/3和N/4(四舍五入)。 // # //#编写一个方法` wonky_coins(n)`返回您需要的硬币数量 //#如果你拿走所有非零硬币并继续给它们喂食,它们就剩下了 //#回到机器里,直到你只剩下零值硬币。 // # //#难度:3/5 // //描述“#不稳定的硬币”做什么 //它“处理一枚价值1”的硬币 //摇摇晃晃的硬币(1)。应该=3 //结束 // //它“处理价值5”的硬币 //摇摇晃晃的硬币(5)。应该=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]] //结束 // //它“处理一枚价值6”的硬币 //不稳定的硬币(6)。应该=15 //结束 // //它“处理被给予零硬币”的事情 //摇摇晃晃的硬币(0)。应该==1 //结束 //结束 功能检查(手动){ 对于(变量i=0;i-1){ 手工拼接(index1,1); } 手推(数学地板(值/2)); 手推(数学地板(值/3)); 手推(数学地板(值/4)); } 回手长度; }

Javascript 当循环不执行或不循环时? //需要'04_摇摇晃晃的硬币' //需要“rspec” // //#卡特西尔瓦式的货币是一件奇怪的事情:他们每个人都有一枚硬币 //#面额(包括零!)。银行里一台摇摇晃晃的兑换机 //#Catsylvania接受任何价值为N的硬币并返回3枚新硬币, //#取值为N/2、N/3和N/4(四舍五入)。 // # //#编写一个方法` wonky_coins(n)`返回您需要的硬币数量 //#如果你拿走所有非零硬币并继续给它们喂食,它们就剩下了 //#回到机器里,直到你只剩下零值硬币。 // # //#难度:3/5 // //描述“#不稳定的硬币”做什么 //它“处理一枚价值1”的硬币 //摇摇晃晃的硬币(1)。应该=3 //结束 // //它“处理价值5”的硬币 //摇摇晃晃的硬币(5)。应该=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]] //结束 // //它“处理一枚价值6”的硬币 //不稳定的硬币(6)。应该=15 //结束 // //它“处理被给予零硬币”的事情 //摇摇晃晃的硬币(0)。应该==1 //结束 //结束 功能检查(手动){ 对于(变量i=0;i-1){ 手工拼接(index1,1); } 手推(数学地板(值/2)); 手推(数学地板(值/3)); 手推(数学地板(值/4)); } 回手长度; },javascript,Javascript,程序正在运行,但由于某些原因,while循环没有循环。我怀疑这种状况有问题。我不确定javaScript是否接受这种条件。但是,在ruby中它是有效的。 有人能帮我解释一下为什么不工作吗?while语句应该这样修复: // require '04_wonky_coins' // require 'rspec' // // # Catsylvanian money is a strange thing: they have a coin for every // # denomination (i

程序正在运行,但由于某些原因,while循环没有循环。我怀疑这种状况有问题。我不确定javaScript是否接受这种条件。但是,在ruby中它是有效的。
有人能帮我解释一下为什么不工作吗?

while语句应该这样修复:

// 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 (check_coins(hand)) {
另外,
check_coins
功能似乎只检查第一枚硬币,应该更改为全部检查。它还返回整数、布尔值或null值中的任意一个,具体取决于条件-它应该只返回整数(第一个非零硬币的索引)或null(没有非零硬币)

固定代码如下所示:

// 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 (check_coins(hand)) {
功能检查(手动){
对于(变量i=0;i0){
返回i;
}
}
返回null;
}
功能wonkyCoins(n){
var-hand=[];
手推(数学地板(n/2));
手推(数学地板(n/3));
手推(数学地板(n/4));
而((indx=支票币(手))!=空){
var值=手[indx];
var index1=手指数(手指数[indx]);
如果(index1>-1){
手工拼接(index1,1);
}
手推(数学地板(值/2));
手推(数学地板(值/3));
手推(数学地板(值/4));
}
回手长度;
}

代码已被清理,以使其更具可读性和惯用Javascript。但是,尚未对其进行测试。

您错过了一个括号来关闭while循环条件参数。您的代码应该是:

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;
}
如果这不起作用,可能是病情本身。尝试:

while(check_coins(hand)){

  ... 

 }

while条件中是否缺少右括号?非常感谢,是的,这是第二个选项。