Self-invoking JS function

96 views Asked by At

I have the following code:

        var currentKey = 0;
        var totalBinaryMultiplesCollection = {};
        for (var row in playField) {
            if (playField.hasOwnProperty(row)) {
                alert(row + " -> " + playField[row]);
                var rowLength = playField[row].length;
                //Call rowCalc function which returns an array with the binary nrs used in calc
                var binaryMultiplesRow = rowCalc(rowLength);
                for(j=0; j < binaryMultiplesRow.length; j++){
                    //Two methods
                    totalBinaryMultiplesCollection[currentKey] = binaryMultiplesRow[j];
                    currentKey+=1;
                }
            }   
        }

I want to change this code to be a self-invoking function. So I added the following things:

(function (){ before the code block.
})(); behind the code block.

This however gives me the following error:

Uncaught TypeError: (intermediate value)(intermediate value)(intermediate value)(...) is not a function(…).

I can't seem to find the problem here. Could someone tell me what is going on?

Current version:

(function () {
        var currentKey = 0;
        var totalBinaryMultiplesCollection = {};
        for (var row in playField) {
            if (playField.hasOwnProperty(row)) {
                alert(row + " -> " + playField[row]);
                var rowLength = playField[row].length;
                //Call rowCalc function which returns an array with the binary nrs used in calc
                var binaryMultiplesRow = rowCalc(rowLength);
                for(j=0; j < binaryMultiplesRow.length; j++){
                    //Two methods
                    totalBinaryMultiplesCollection[currentKey] = binaryMultiplesRow[j];
                    currentKey+=1;
                }
            }   
        }
    })();

and the rowCalc function being called:

var rowCalc = function(rowlength){
        var currentRowCollection = [];
        switch(rowlength) {
        case 1:
            currentRowCollection.push(1);
            break;
        case 2:
            currentRowCollection.push(2);
            break;
        case 3:
            currentRowCollection.push(1);
            currentRowCollection.push(2);
            break;
        case 4:
            currentRowCollection.push(4);
            break;
        case 5:
            currentRowCollection.push(1);
            currentRowCollection.push(4);
            break;
        case 6:
            currentRowCollection.push(2);
            currentRowCollection.push(4);
        case 7:
            currentRowCollection.push(2);
            currentRowCollection.push(4);
            currentRowCollection.push(1);
            break;
        default:
            alert("You made a mistake!")
        }
        return currentRowCollection; 
    }
1

There are 1 answers

0
Bergi On

There are two missing semicolons in your rowCalc function, the second of which is causing the error:

var rowCalc = function(rowlength){
    var currentRowCollection = [];
    switch(rowlength) {
    case 1:
        currentRowCollection.push(1);
        break;
    case 2:
        currentRowCollection.push(2);
        break;
    case 3:
        currentRowCollection.push(1);
        currentRowCollection.push(2);
        break;
    case 4:
        currentRowCollection.push(4);
        break;
    case 5:
        currentRowCollection.push(1);
        currentRowCollection.push(4);
        break;
    case 6:
        currentRowCollection.push(2);
        currentRowCollection.push(4);
    case 7:
        currentRowCollection.push(2);
        currentRowCollection.push(4);
        currentRowCollection.push(1);
        break;
    default:
        alert("You made a mistake!");
//                                  ^
    }
    return currentRowCollection; 
}; /*
 ^  */