Created
June 7, 2022 18:28
-
-
Save Wittiest/8816744943f1bedc4aa5a02ef4f45b23 to your computer and use it in GitHub Desktop.
Bad code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Animal { | |
constructor(age) { | |
this.age = age | |
} | |
} | |
class Dog extends Animal { | |
constructor(amountOfLegs, age) { | |
super(age) | |
this.amountOfLegs = amountOfLegs | |
} | |
// Print out the wrod bark | |
bark() { | |
const BARK = 'Bark' | |
console.log(`${BARK}`) | |
} | |
} | |
class Cat extends Animal { | |
constructor(amountOfLegs, age) { | |
super(age) | |
this.amountOfLegs = amountOfLegs | |
} | |
// Print out the wrod meow | |
meow() { | |
console.log('Meow') | |
} | |
} | |
class Farm { | |
constructor(animals) { | |
this.animals = animals | |
console.log('TEST ARRAY', this.animals) | |
} | |
// Return a count of the animals for each age | |
// I think no animal will be older than 10 so this should be fine | |
gettFarmAnimalAgecCount() { | |
let MAX_AGE_COUNT = 10 | |
let animalsByAge = {} | |
for (let age = 0; age < MAX_AGE_COUNT; age++) { | |
const c = this.getAnimalsOfAge(age) | |
// We only want a key / value pair for animals of the ages that exist, so dont save for cases of 0 | |
if (c > 0) { | |
animalsByAge[age] = c | |
} | |
} | |
return animalsByAge | |
} | |
// Get count of animals of age | |
getAnimalsOfAge(age) { | |
let m = 0 | |
for (let indexOfAnimalInArray = 0; indexOfAnimalInArray < this.animals.length; indexOfAnimalInArray++) { | |
if (this.animals[indexOfAnimalInArray].age == age) { | |
m++ | |
} | |
} | |
if (m <= 0) { | |
return 0 | |
} | |
return m | |
} | |
} | |
let dog = new Dog(1, 1) | |
let dog2 = new Dog(1, 2) | |
let cat = new Cat(1, 2) | |
let farm = new Farm([dog2, dog, cat]) | |
console.log(farm.gettFarmAnimalAgecCount()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment