0

I am receiving a type error stating that the array testA[i] is undefined whenever I add an input into the html page. I have the array set and i'm trying to add the value of currency to the array using the push method to add to the second part of the array i.e([0][currency])

function Test() {

var testA = [];
for (i = 0; i < 4; i++) {
        this.currency = prompt("Please enter a 3-letter currency abbreviation", "");
        testA[i].push(currency);
        }
}
var index = new Test();

enter image description here

any help as to why the array is undefined would be appreciated.

Note: I have now tried both testA.push(currency) and testA[i] = this.currency, and I still get the same error as before.

Note: the final version should have it loop through 4 different questions asked and each time adding these into an array. At the end of the loop a new variant of the array should be made and the new set of data entered will be added to it. something like for(i = 0; i < 4; i++) { testA[i] = i; for(j = 0; j < 4; j++) { this.currency = prompt("Please enter a 3-letter currency abbreviation", ""); testA[i][j] = this.currency; } }

but at this point in time I'm just trying to get it to work.

soul6942
  • 67
  • 9
  • forgot to mention, this needs to loop for 4 different parts. hence I need the currency value to be at the index. testA[i][0]<- here. then once the loop hits the end, i goes up by 1 and it asks the question again. after 4 times I should have testA[0][currency] testA[1][currency] testA[2][currency] testA[3][currency] – soul6942 Sep 12 '17 at 08:53

4 Answers4

4

You don't use the push method on a index. You use it on the array itself.

Replace this

testA[i].push(currency);

With this

testA.push(currency);
Lars Peterson
  • 1,508
  • 1
  • 10
  • 27
1

You need to perform push operation on the array directly.

testA.push(currency);

By executing testA[index] you will receive hold value. In JS it will always return undefined, if index is greater than array length.

Because your array is empty as the beginning, you are always receiving undefined.

Beri
  • 11,470
  • 4
  • 35
  • 57
1

You are mixing up two different implementation.

Either you use direct assignation.

var testA = new Array(4);

for (i = 0; i < 4; i += 1) {
    this.currency = prompt('...', '');

    testA[i] = this.currency;
}

Either you push new values into the array.

var testA = [];

for (i = 0; i < 4; i += 1) {
    this.currency = prompt('...', '');

    testA.push(this.currency);
}

You should use the second one, which is the most simple soluce.

Orelsanpls
  • 22,456
  • 6
  • 42
  • 69
1
testA[i] = this.currency OR testA.push(this.currency) 

Use Modified function below

function Test() {
       var testA = [];
            for (i = 0; i < 4; i++) {
                    this.currency = prompt("Please enter a 3-letter currency abbreviation", "");
                    testA[i] = this.currency; // use this.currency here if you 
                    }
            console.log(testA);
            }

var index = new Test();
Rohan Fating
  • 2,135
  • 15
  • 24