0

I'm having a problem getting .push to add an additional value to an array on any push beyond the first.

var player_score = [];

function updatePlayerScore(x) {
    player_score.push(x);
    console.log(player_score);
}

The first time I call updatePlayerScore(x) with a value of 2, the array is successfully updated with [2] and displays in the console.

The next time I call updatePlayerScore(x) with any value (i.e. 4) an error is thrown "Uncaught TypeError: player_score.push is not a function"

4
  • It's most likely correct, in that .push() is not a function. Is there changing the reference of player_score? Commented Sep 24, 2016 at 4:45
  • How do you call the function? Commented Sep 24, 2016 at 4:46
  • Thank you for the direction - there was a change of reference to player_score in a seperate function that was causing the issue. I was able to comment out the line of code and the push worked correctly. Commented Sep 24, 2016 at 4:53
  • Just a wild guess but maybe the problem is that player_score.push is not a function. Commented Sep 24, 2016 at 5:31

1 Answer 1

1

It is absolutely working fine unless you have changed the player_score reference between two calls. Please check your code for usage of player_score variable.

var player_score = [];

function updatePlayerScore(x) {
    player_score.push(x);
    console.log(player_score);
}
updatePlayerScore(2);
updatePlayerScore(4);

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

1 Comment

Thank you for the help - I was able to find the issue further down in another function.

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.