Junior — Senior
Function that immediately logs all meowing cats to the console
livecode
Task condition
In the pet hotel, only cats and dogs are accepted. On this day, their numbers are equal – 5 each. However, for some reason, all the cats are meowing. The function fetchAnimals() simulates retrieving a list of all animals and returns it with a one-second delay. The function animalRecognizer() after a short pause determines the type of animal and adds it to the object. It is required to implement a function that, after receiving the data, immediately logs only those entries where the animal is a cat and its voice is meow.
const ANIMAL_TYPE = {
CAT: 'cat',
DOG: 'dog',
};
const ANIMAL_VOICE = {
MEOW: 'meow',
WOOF: 'woof',
};
const TIMEOUT = {
1000: 1000,
75: 75,
};
class DataItem {
constructor(name, voice) {
this.name = name;
this.voice = voice;
}
makeSound() {
if (this.type) {
console.log(this.name, '-', this.voice);
} else {
console.log('Ooops');
}
}
}
const ANIMALS_TYPE = {
Molly: ANIMAL_TYPE.CAT,
Felix: ANIMAL_TYPE.DOG,
Smudge: ANIMAL_TYPE.CAT,
Sooty: ANIMAL_TYPE.CAT,
Tigger: ANIMAL_TYPE.CAT,
Charlie: ANIMAL_TYPE.DOG,
Alfie: ANIMAL_TYPE.CAT,
Oscar: ANIMAL_TYPE.DOG,
Millie: ANIMAL_TYPE.DOG,
Misty: ANIMAL_TYPE.CAT,
};
const DATA = [
new DataItem('Molly', ANIMAL_VOICE.MEOW),
new DataItem('Felix', ANIMAL_VOICE.WOOF),
new DataItem('Smudge', ANIMAL_VOICE.MEOW),
new DataItem('Sooty', ANIMAL_VOICE.WOOF),
new DataItem('Tigger', ANIMAL_VOICE.MEOW),
new DataItem('Charlie', ANIMAL_VOICE.WOOF),
new DataItem('Alfie', ANIMAL_VOICE.MEOW),
new DataItem('Oscar', ANIMAL_VOICE.WOOF),
new DataItem('Millie', ANIMAL_VOICE.WOOF),
new DataItem('Misty', ANIMAL_VOICE.MEOW),
];
function fetchAnimals() {
return new Promise(resolve => {
setTimeout(resolve, TIMEOUT[1000], DATA);
});
}
function animalRecognizer(dataItem) {
const animalWithType = Object.create(dataItem);
animalWithType.type = ANIMALS_TYPE[dataItem.name];
return new Promise(resolve => {
setTimeout(resolve, TIMEOUT[75], animalWithType);
});
}
// Here, you need to implement a function that will get the list of animals, recognize their types, and immediately log only the meowing cats.