Suhaib Ahmad
Suhaib Ahmad

Reputation: 526

TypeError: record is undefined

I am making a JS game, and i have to update the highscores and display them using cookies. The functions below are in a file highscore.js

function getHighScoreTable() {
    var table = new Array();
    for (var i = 0; i < 10; i++) {
        // Contruct the cookie name
        var cookie_name = "player" + i;
        // Get the cookie value using the cookie name
        var cookie_value = getCookie(cookie_name);
        // If the cookie does not exist exit from the for loop
        if (!cookie_value) {
            break;
        }
        // Extract the name and score of the player from the cookie value
        var value_array = cookie_value.split("~");
        var pname = value_array[0];
        var pscore = value_array[1];
        // Add a new score record at the end of the array
        table.push(new ScoreRecord(pname, pscore));
    }
    return table;
}
//
// This function stores the high score table to the cookies
//
function setHighScoreTable(table) {
    for (var i = 0; i < 10; i++) {
        // If i is more than the length of the high score table exit
        // from the for loop
        if (i >= table.length) break;
        // Contruct the cookie name
        var cookie_name = "player" + i;
        var record = table[i];
        var cookie_value = record.name + "~" + record.score; // **error here = TypeError: record is undefined**
        // Store the ith record as a cookie using the cookie name
        setCookie(cookie_name, cookie_value);
    }
}

in my game.js, i have a function gameOver() which handles the highscore etc and clearing the gametimers.

function gameOver() {
    clearInterval(gameInterval);
    clearInterval(timeInterval);
    alert("game over!");
    var scoreTable = getHighScoreTable();
    var record = ScoreRecord(playerName, score);
    var insertIndex = 0;
    for (var i = 0; i < scoreTable.length; i++) {
        if (score >= scoreTable[i].score) {
            insertIndex = i;
            break;
        }
    }
    if (scoreTable.length == 0) {
        scoreTable.push(record);
    } else {
        scoreTable.splice(insertIndex, 0, record);
    }
    setHighScoreTable(scoreTable);
    showHighScoreTable(scoreTable);
}

When the gameover is called in game, error occurs in setHighScoreTable(table) and the error is that record (i.e. table[i]) is undefined. Need help in this bug.

Upvotes: 0

Views: 1771

Answers (1)

Tibrogargan
Tibrogargan

Reputation: 4603

Assuming ScoreRecord is defined something like this:

function ScoreRecord(name, score) {
    this.name = name;
    this.score = score;
}

The problem is you are doing:

record = ScoreRecord(playerName, score);

This will just call the constructor as if it were a function - but it returns nothing. Just add the new keyword to create a new object instead

record = new ScoreRecord(playerName, score);

You could also do something like this to prevent the constructor being called as a normal function:

function ScoreRecord(name, score) {
    "use strict"

    if (!(this instanceof ScoreRecord)) {
        throw new Error("ScoreRecord must be called with the new keyword");
    }
    this.name = name;
    this.score = score;
}

Upvotes: 1

Related Questions