1
function Type(name, effectivenessData) {
    this.name = name;
    this.effectivenessData = effectivenessData;
}

var types = [
    new Type("Fire", ["X", "X", "O", "", "O"]),
    new Type("Water", ["O", "X", "X", "", ""]),
    new Type("Grass", ["X", "O", "X", "", ""]),
    new Type("Electric", ["", "O", "X", "X", ""]),
    new Type("Ice", ["X", "X", "O", "", "X"])
];

var getTypes = function getTypeNames() {
    return types.map(t => t.name);
}

My solutions: Hi all, I am using inner for loop to access effectivenessData but I get error saying length undefined. Can please somebody help me to understand how can I access the data here..or what am I doing wrong

for (var i =0; i < getTypes.length; i++) {
    console.log(getTypes[i].toString());

    for(var j=0; j< getTypes[i].effectivenessData; j++) {
console.log(getTypes[i].effectivenessData.[j]) // When I console log to check the data I get length undefined 
}
}

4 Answers4

1

You can use something like this to loop through:

function Type(name, effectivenessData) {
   this.name = name;
   this.effectivenessData = effectivenessData;
}

var types = [
    new Type("Fire", ["X", "X", "O", "", "O"]),
    new Type("Water", ["O", "X", "X", "", ""]),
    new Type("Grass", ["X", "O", "X", "", ""]),
    new Type("Electric", ["", "O", "X", "X", ""]),
   new Type("Ice", ["X", "X", "O", "", "X"])
];

for(var i = 0; i < types.length; i++){
   console.log(types[i].effectivenessData)
};
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
0

You should be using getTypes()[i] instead of getTypes[i] since it is a function.

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
Nick Prozee
  • 2,823
  • 4
  • 22
  • 49
0

It looks like you're trying to print the types' names in one loop and effectivenessData in another. You're attempting to index into getTypes as if it were an array, but it's a function which doesn't support such operations. Use getTypes to retrieve the array, then call forEach on the array to iterate over its elements:

function Type(name, effectivenessData) {
    this.name = name;
    this.effectivenessData = effectivenessData;
}

var types = [
    new Type("Fire", ["X", "X", "O", "", "O"]),
    new Type("Water", ["O", "X", "X", "", ""]),
    new Type("Grass", ["X", "O", "X", "", ""]),
    new Type("Electric", ["", "O", "X", "X", ""]),
    new Type("Ice", ["X", "X", "O", "", "X"])
];

var getTypes = function () {
    return types.map(t => t.name);
};

getTypes().forEach(e => console.log(e));

types.forEach(e => console.log(e.effectivenessData));

// or, using a traditional loop:
for (let i = 0; i < types.length; i++) {
  console.log(types[i]);
}

As can be seen here, getTypes is a candidate for renaming (because it really returns type names) or removal (because it doesn't seem to add much to the code as it stands).

ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • 1
    Don't use `forEach(console.log)`, use `forEach(x => console.log(x))` – Barmar Aug 24 '18 at 22:56
  • May I ask why you suggest this? – ggorlen Aug 24 '18 at 22:57
  • https://stackoverflow.com/questions/9639167/why-doesnt-console-log-work-when-passed-as-a-parameter-to-foreach – Barmar Aug 24 '18 at 22:57
  • That explains why it gets an error in some browsers. But even when it works, it prints extra output, because `forEach()` passes 3 arguments to the callback: the array element, the array index, and the whole array, so each of these gets logged. – Barmar Aug 24 '18 at 22:58
  • Ah, OK. Got it. – ggorlen Aug 24 '18 at 22:59
0

getTypes must be invoked e.g getTypes() and its return value should be stored in a var so that the iteration code is cleaner.