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
}
}
It looks like you're trying to print the types' name
s 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).