Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Program.js
const ANIMALS = [
{
date: '2019-02-10',
location: 'A',
code: 1,
},
{
date: '2019-03-17',
location: 'B',
code: 15,
},
{
date: '2019-03-17',
location: 'A',
code: 45,
},
{
date: '2019-03-17',
location: 'A',
code: 354,
},
{
date: '2019-03-17',
location: 'B',
code: 34535,
},
{
date: '2019-03-17',
location: 'A',
code: 345,
},
{
date: '2019-03-17',
location: 'B',
code: 1001,
},
]
function MammalsPercentage(animals) {
//we save the animal array length
//we calcule the length of the array once
let lengthJson = animals.length
let mammalAnimal = 0
let mammalsPercentage
animals.forEach(animal => {
if (animal.code > 1000) {
mammalAnimal++
}
})
// we calcule the percentage of mammal animals
mammalsPercentage = (mammalAnimal * 100) / lengthJson
// we print in console the number of mammal animals (whose code above 1000)
console.log(mammalsPercentage + ' %')
}
function OftenMammalDay(animals) {
//we save the animal array length
//date more repeated in location A and with different code
let daySelected
let mammalsPercentage
//we apply a filter to get an array just located in place A
let animalsLocA = animals.filter(
animalLocationA => animalLocationA.location === 'A'
)
// we make an auxiliar copy of array AnimalsLocA to check both arrays and detect the repeated
let animalsLocACopy = animalsLocA
animalsLocA.forEach(animal => {
animalsLocACopy.forEach(animalCopy => {
//
if (animal.date === animalCopy.date) {
daySelected = animal.date
}
})
})
console.log(daySelected)
}
MammalsPercentage(ANIMALS)
OftenMammalDay(ANIMALS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.