1

I was wondering what the structured equivalent of a continue statement is in JavaScript? I am trying to get rid of a continue statement but not sure how. Can anyone point me in the right direction? Thanks!

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];
    };
}

4 Answers4

2

continue statement is valid in JavaScript. You can use it as you would in any language.

After saying that, you can read this interesting discussion on why you might want to avoid it, and how.

Community
  • 1
  • 1
e-shfiyut
  • 2,880
  • 1
  • 27
  • 29
1

else if pair will help you here:

    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);
        }
    }
SergeyS
  • 3,410
  • 15
  • 27
0

This should hold true for any language and not just java script

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];
    };
}
saurabh baid
  • 1,679
  • 1
  • 10
  • 19
-3

One option is:

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);
});

Apparently some people think return terminates everything... return stops the function running for the current element, making the next iteration start right away, as would do continue. I'm not saying this is a better option than just using continue, but it's definitively an alternative.

Guig
  • 8,612
  • 5
  • 47
  • 99
  • `return` ends the function, but `continue` goes on with the for loop. – Nina Scholz Sep 17 '16 at 08:32
  • `returns` ends the function in the loop and start the next iteration of the loop. Isn't it what `continue` is doing? – Guig Sep 17 '16 at 08:34
  • no, `return` terminates the function (and). no more iteration. `continue` jumps to the start of the for loop and skips the part to the end of the for loop. – Nina Scholz Sep 17 '16 at 08:37
  • this is incorrect. I suggest you open your console and run something like `var a = []; [1, 2, 3, 4].forEach(function(i) { if (i % 2 == 0) { return; } a.push(i); }` and see what's in `a` (hint: it's `[1, 3]`) – Guig Sep 17 '16 at 08:40
  • I agree `return` only stops the current iteration, but isn't what `continue` does? From the doc (http://www.w3schools.com/js/js_break.asp), it says "The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop." – Guig Sep 17 '16 at 08:49
  • sure, but I'm done doing a `return` in a for loop, but within the forEach loop. Run this: https://jsfiddle.net/cbm5uL2m/1/ I agree doing a `return` in the for loop would not work. But this not what I'm suggesting – Guig Sep 17 '16 at 08:57
  • 1
    `forEach()` and `return` is certainly equivalent to `for` and `continue`, but I got the impression the OP wants to avoid jumping out of the middle like that. (Not sure if it is the OP's goal, but I know some people believe that a function should be structured with a single return at the end and no jumping statements like `continue` or `break` - I disagree with those people.) – nnnnnn Sep 17 '16 at 09:15
  • :) I similarly think that returning asap helps lower the cognitive charge of remembering what nested condition you're in. Anyway thanks for confirming the obvious on `forEach + return == for + continue` ! – Guig Sep 17 '16 at 09:19