0

I've been trying to figure this problem out for hours but I'm having absolutely no luck.

The programs runs through the first iteration without any issues (numbers are spliced into the array properly and the results are printed to console perfectly) but on the second run I get this error:

Uncaught TypeError: Cannot call method 'splice' of undefined

Nothing at all is printed to the console log at this point.

Here's the first function:

var gameRound = [
    [],
    []
];
fight();

function fight() {
    var r = 10;
    var towel = false;
    var i = 1;
    while (i <= r && towel == false) {
        round(i);
        console.log("ROUND ", i);
        console.log(boxerA[0]);
        console.log("Punches Thrown: ", gameRound[i][0]);
        console.log("Punches Landed: ", gameRound[i][1]);
        console.log("Jabs Thrown: ", gameRound[i][2]);
        console.log("Jabs Landed: ", gameRound[i][3]);
        console.log("Power Thrown: ", gameRound[i][4]);
        console.log("Power Landed: ", gameRound[i][5]);
        console.log("Fatigue: ", boxerA[28]);
        console.log("Damage: ", boxerA[29]);
        console.log(boxerB[0]);
        console.log("Punches Thrown: ", gameRound[i][6]);
        console.log("Punches Landed: ", gameRound[i][7]);
        console.log("Jabs Thrown: ", gameRound[i][7]);
        console.log("Jabs Landed: ", gameRound[i][9]);
        console.log("Power Thrown: ", gameRound[i][10]);
        console.log("Power Landed: ", gameRound[i][11]);
        console.log("Fatigue: ", boxerB[28]);
        console.log("Damage: ", boxerB[29]);
        i++;
    }
}

And the function and line where I get the error listed:

function round(i)
{
// ...
gameRound[i].splice(0,0,numPunchesTA,numPunchesLA,numJabsTA,numJabsLA,numPowerTA,      numPowerLA, numPunchesTB, numPunchesLB,numJabsTB,numJabsLB,numPowerTB,numPowerLB);

}

When I test it the variable i is still counted up to "2" so I'm not sure why the next row isn't being spliced into the array.

1
  • You should start to indent your code, it will make it easier to read. Commented Nov 10, 2013 at 16:14

1 Answer 1

1

Array indexes start at 0. Your array has to elements at position 0 and 1. Hence if i is increased to 2, you get an error because gameRound[2] does not exist.

If you want to add a new array to the outer array, you have to assign it explicitly:

gameRound[i] = [numPunchesTA,numPunchesLA, ...];

Or you have to initialize gameRound with 10 empty arrays.

Or you have to start counting from 0 until i reaches 2:

var i = 0;
while (i < 2 && towel == false) {

Since you didn't really tell us what exactly you are trying to achieve with the code, it's difficult to say what the appropriate solution would be.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.