I have an array of objects
let pets = [
{type: 'dog', name: 'Bernie'},
{type: 'cat', name: 'Bonnie'},
{type: 'lizard', name: 'heartache'},
{type: 'dog', name: 'spinach'}
];
I'm writing a function that takes in this array and returns true if all the types are the same and false if not.
Here is what i've tried:
for (let i = 0; i < pets.length; i++) {
if (pets[i].type != pets[i + 1].type) {
return false;
} else {
return true;
}
}
I thought this would work. Isn't this iterating through the loop and comparing the first object's type with the next?
I've also tried
for(let i = 0;i < pets.length;i++){
let petOne = pet[i];
let petNext = pet[i+1];
if(petOne[i].type != petNext[i].type){
return false;
}else{
return true;
}
}
I feel like I'm close. How would I go about comparing the pet types and returning true or false.