Seeking to identify the type of shape based on the number of sides passed in. The code provided only recognizes the first index, which is a triangle. I suspect this is because I am not correctly comparing the number of sides to the sides property in the array. I attempted to use filter
, forEach
, and map
methods but got stuck. I appreciate any assistance.
var Shape = function(sides) {
this.sides = sides;
if (this.sides < 3 || typeof(this.sides) !== 'number'){
this.sides = null;
}
};
Shape.prototype.getType = function(sides){
var shapes = [{type: "triangle", sides: 3}, {type: "quadrilateral", sides: 4}, {type: "pentagon", sides: 5}, {type: "hexagon", sides:6}, {type: "heptagon", sides: 7}, {type: "octagon", sides: 8}, {type: "nonagon", sides: 9}, {type: "decagon", sides: 10}];
for (var i = 0; i < shapes.length; i++){
console.log(shapes[i].sides);
var sideExists = shapes.indexOf(shapes[i].sides) > -1;
if (sides === sideExists){
return shapes[i].type;
}else{
return 'Could not determine type';
}
}
};