Uncaught TypeError: undefined is not a function

user3100660

Hi everyone I'm currently a student working on a project out of my book, here is the code I am having the issue with.

for (var i = 0; i < numAsteroids; i++) {
    var radius = 5+(Math.random()*10);
    var x = canvasWidth+radius+Math.floor(Math.random()*canvasWidth);
    var y = Math.floor(Math.random()*canvasHeight);
    var vX = -5-(Math.random()*5);
    
    asteroids.push(new Asteroid(x, y, radius, vX));***<---line 21***
};

Now when I run this is keeps telling me I have an:

Uncaught TypeError: undefined is not a function line 21.

But I have the code exactly how it is in my book that's what I don't understand. If anyone could help me with this problem it would be great!

jfriend00

If the error is coming from this line because of the .push() method:

asteroids.push(new Asteroid(x, y, radius, vX));

Then, the issue is that asteroids has not been initialized as an array so thus it doesn't have a method .push().

You can initialize it like this:

var asteroids = [];
for (var i = 0; i < numAsteroids; i++) {
    var radius = 5+(Math.random()*10);
    var x = canvasWidth+radius+Math.floor(Math.random()*canvasWidth);
    var y = Math.floor(Math.random()*canvasHeight);
    var vX = -5-(Math.random()*5);

    asteroids.push(new Asteroid(x, y, radius, vX));
}

It might also be possible to get this type of error if the Asteroid constructor function is not defined.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Uncaught TypeError:Undefined is not a function

From Dev

Uncaught TypeError: undefined is not a function

From Dev

Uncaught TypeError: undefined is not a function

From Dev

Uncaught TypeError: undefined is not a function

From Dev

Uncaught TypeError: undefined is not a function

From Dev

Uncaught TypeError: undefined is not a function?

From Dev

Uncaught TypeError: undefined is not a function $

From Dev

Twitter Uncaught TypeError: undefined is not a function

From Dev

Reactjs: Uncaught TypeError: undefined is not a function

From Dev

Uncaught TypeError: undefined is not a function - Javascript

From Dev

uncaught typeerror undefined is not a function on .empty()

From Dev

uncaught typeerror undefined is not a function in javascript

From Dev

Uncaught TypeError: Undefined is not a function on indexOf

From Dev

Uncaught TypeError: undefined is not a function for datatables

From Dev

Uncaught TypeError: undefined is not a function 'appendTo'

From Dev

Uncaught TypeError: undefined is not a function in datepicker

From Dev

Uncaught TypeError: undefined is not a function with jQuery

From Dev

Uncaught TypeError: undefined is not a function - checkValidity

From Dev

Uncaught TypeError: undefined is not a function:67

From Dev

Uncaught TypeError: undefined is not a function for datatables

From Dev

Another: uncaught typeerror undefined is not a function

From Dev

uncaught typeerror undefined is not a function on .empty()

From Dev

Uncaught TypeError: undefined is not a function in datepicker

From Dev

Uncaught TypeError: undefined is not a function - Slider

From Dev

Uncaught TypeError: undefined is not a function in RefluxJS

From Dev

Uncaught TypeError: undefined is not a function - datatables

From Dev

Uncaught TypeError undefined is not a function anonymous function

From Dev

Uncaught TypeError: undefined is not a function (anonymous function) in Wordpress

From Dev

Uncaught TypeError undefined is not a function anonymous function

Related Related

  1. 1

    Uncaught TypeError:Undefined is not a function

  2. 2

    Uncaught TypeError: undefined is not a function

  3. 3

    Uncaught TypeError: undefined is not a function

  4. 4

    Uncaught TypeError: undefined is not a function

  5. 5

    Uncaught TypeError: undefined is not a function

  6. 6

    Uncaught TypeError: undefined is not a function?

  7. 7

    Uncaught TypeError: undefined is not a function $

  8. 8

    Twitter Uncaught TypeError: undefined is not a function

  9. 9

    Reactjs: Uncaught TypeError: undefined is not a function

  10. 10

    Uncaught TypeError: undefined is not a function - Javascript

  11. 11

    uncaught typeerror undefined is not a function on .empty()

  12. 12

    uncaught typeerror undefined is not a function in javascript

  13. 13

    Uncaught TypeError: Undefined is not a function on indexOf

  14. 14

    Uncaught TypeError: undefined is not a function for datatables

  15. 15

    Uncaught TypeError: undefined is not a function 'appendTo'

  16. 16

    Uncaught TypeError: undefined is not a function in datepicker

  17. 17

    Uncaught TypeError: undefined is not a function with jQuery

  18. 18

    Uncaught TypeError: undefined is not a function - checkValidity

  19. 19

    Uncaught TypeError: undefined is not a function:67

  20. 20

    Uncaught TypeError: undefined is not a function for datatables

  21. 21

    Another: uncaught typeerror undefined is not a function

  22. 22

    uncaught typeerror undefined is not a function on .empty()

  23. 23

    Uncaught TypeError: undefined is not a function in datepicker

  24. 24

    Uncaught TypeError: undefined is not a function - Slider

  25. 25

    Uncaught TypeError: undefined is not a function in RefluxJS

  26. 26

    Uncaught TypeError: undefined is not a function - datatables

  27. 27

    Uncaught TypeError undefined is not a function anonymous function

  28. 28

    Uncaught TypeError: undefined is not a function (anonymous function) in Wordpress

  29. 29

    Uncaught TypeError undefined is not a function anonymous function

HotTag

Archive