diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000000..b4ec085338 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,17 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "skipFiles": [ + "/**" + ], + "program": "${workspaceFolder}\\challenges\\arrays-callbacks.js" + } + ] +} \ No newline at end of file diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 12af878ceb..4d04af2fd8 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -20,17 +20,27 @@ const zooAnimals = [ The zoos want to display both the scientific name and the animal name in front of the habitats. Populate the displayNames array with only the animal_name and scientific_name of each animal. displayNames will be an array of strings, and each string should follow this pattern: "Name: Jackal, asiatic, Scientific: Canis aureus." */ -const displayNames = []; -console.log(displayNames); +var displayNames = [] + + zooAnimals.forEach((names) => { + + displayNames.push(zooAnimals.animal_name + zooAnimals.scientific_name) + console.log(`Animal Name: ${names.animal_name}, Scientific Name: ${names.scientific_name}`)}); /* Request 2: .map() The zoos need a list of all their animal's names (animal_name only) converted to lower case. Using map, create a new array of strings named lowCaseAnimalNames, each string following this pattern: "jackal, asiatic". Log the resut. */ - const lowCaseAnimalNames = []; -console.log(lowCaseAnimalNames); + +zooAnimals.map((Aname,index) => { + + lowCaseAnimalNames.push(zooAnimals[index].animal_name); + + console.log(`Animal Name: ${Aname.animal_name.toLowerCase()}`) + } +); /* Request 3: .filter() @@ -38,14 +48,31 @@ The zoos are concerned about animals with a lower population count. Using filter */ const lowPopulationAnimals = []; + +zooAnimals.filter((pop) => { + + if (pop.population < 5); + { + lowPopulationAnimals.push(pop.animal_name); + } + + +}) + console.log(lowPopulationAnimals); /* Request 4: .reduce() The zoos need to know their total animal population across the United States. Find the total population from all the zoos using the .reduce() method. Remember the reduce method takes two arguments: a callback (which itself takes two args), and an initial value for the count. + */ -let populationTotal = 0; + +let populationTotal = zooAnimals.reduce((total, zoo) => { +return total += zoo.population; + +}, 0) + console.log(populationTotal); @@ -58,6 +85,15 @@ console.log(populationTotal); * The consume function should return the invocation of cb, passing a and b into cb as arguments */ +function consume (a,b,cb) +{ + cb () + { + a = arg1; + b = arg2; + } + +} /* Step 2: Create several functions to callback with consume(); * Create a function named add that returns the sum of two numbers