2016-09-17 11 views
1

continueステートメントの構造化された同等のものがJavaScriptにあるのだろうか?私はcontinueステートメントを取り除こうとしていますが、どうしたらよいか分かりません。誰かが私を正しい方向に向けることができますか?ありがとう!JavaScriptのcontinueステートメントに相当する

function Hand() { 
    this.cards = new Array(); 

    this.addOneCard = function(card) { 
     this.cards.push(card); 
    } 

    this.evaluateHand = function(){ 
     // Needs to handle two aces better 
     var total1 = new Number; 
     var total2 = new Number; 

     for(var i in this.cards) { 
      if (this.cards[i].value == "A") { 
       total1 += 1; 
       total2 += 11; 
       continue; 
      } 
      if (isNaN(this.cards[i].value * 1)) { 
       total1 += 10; 
       total2 += 10; 
       continue; 
      } 
      total1 += Number(this.cards[i].value); 
      total2 += Number(this.cards[i].value); 
     } 
     return [total1, total2]; 
    }; 
} 
+1

なぜ変更が必要ですか? 'continue'は言語の一部であり、あなたの必要性に合っています。 –

答えて

1

else ifペアは、ここであなたを助ける:

for(var i in this.cards) { 
     if (this.cards[i].value == "A") { 
      total1 += 1; 
      total2 += 11; 
     } 
     else if (isNaN(this.cards[i].value * 1)) { 
      total1 += 10; 
      total2 += 10; 
     } 
     else { 
      total1 += Number(this.cards[i].value); 
      total2 += Number(this.cards[i].value); 
     } 
    } 
-2

1つのオプションは次のとおりです。どうやら一部の人々はreturnがすべてを終了

this.cards.forEach(function(card) { 
    if (card.value == "A") { 
     total1 += 1; 
     total2 += 11; 
     return; 
    } 
    if (isNaN(card.value * 1)) { 
     total1 += 10; 
     total2 += 10; 
     return; 
    } 
    total1 += Number(card.value); 
    total2 += Number(card.value); 
}); 

だと思う... returnは、現在稼働し機能を停止次の反復を即座に開始します(continue)。私はこれが単にcontinueを使用するより良い選択肢であると言っているわけではありませんが、それは確かに選択肢です。

+0

'return'は関数を終了させますが、' continue'はforループで続けます。 –

+0

'returns'はループ内の関数を終了し、ループの次の繰り返しを開始します。 「続ける」ことがやっているのではないですか? – Guig

+0

いいえ、 'return'は関数(と)を終了します。もはや繰り返しはありません。 'continue'はforループの先頭にジャンプし、その部分をforループの最後にスキップします。 –

0

これは

function Hand() { 
    this.cards = new Array(); 

    this.addOneCard = function(card) { 
     this.cards.push(card); 
    } 

    this.evaluateHand = function(){ 
     // Needs to handle two aces better 
     var total1 = new Number; 
     var total2 = new Number; 

     for(var i in this.cards) { 
      if (this.cards[i].value == "A") { 
       total1 += 1; 
       total2 += 11; 
      } 
      else if (isNaN(this.cards[i].value * 1)) { 
       total1 += 10; 
       total2 += 10; 
      } 
      else { 
       total1 += Number(this.cards[i].value); 
       total2 += Number(this.cards[i].value); 
      } 
     } 
     return [total1, total2]; 
    }; 
} 
2

continue文がJavaScriptで有効なスクリプトだけでJavaのいずれかの言語のための真のではない保持する必要があります。どの言語でも使用できます。

と言ったら、これを避ける理由とその理由をinteresting discussionでお読みください。

関連する問題