hudsond7
hudsond7

Reputation: 706

Cannot push to array, undefined. Javascript

I am working on a small project that requires me to push numbers/operators into an array. When doing so the JavaScript console is telling me it is not possible to push to an undefined array. I am unsure as to what this means. Here is a small snippet of my code.

run: function(clicked){
        var numbers = [];
        var operations = [];

        switch(clicked){
            case "0":
            case "1":
            case "2":
            case "3":
            case "4":
            case "5":
            case "6":
            case "7":
            case "8":
            case "9":
                document.getElementById("dis1").innerHTML += clicked;
                break;
            case "point":
                document.getElementById("dis1").innerHTML += ".";
                break;
            case "mul":
                this.numbers.push(document.getElementById("dis1").innerHTML);
                document.getElementById("dis1").innerHTML = "";
                document.getElementById("dis1").innerHTML += "*";
                console.log(numbers);
                break;

As mentioned this is only a small snippet hence the unclosed functions etc.

Upvotes: 0

Views: 83

Answers (1)

dehrg
dehrg

Reputation: 1741

You are misusing this

You should use numbers.push not this.numbers.push

Upvotes: 2

Related Questions