Skip to content

Instantly share code, notes, and snippets.

@natafaye
Created July 14, 2021 00:58
Show Gist options
  • Save natafaye/d1278f96c7fd0322e6ffab82736e122f to your computer and use it in GitHub Desktop.
Save natafaye/d1278f96c7fd0322e6ffab82736e122f to your computer and use it in GitHub Desktop.
Javascript Week 4 Code From Class
/*************** Is Yes No ***************/
// The original function
function isYesNo(answer) {
return answer.toLowerCase() === "yes" || answer.toLowerCase() === "no";
}
// Converted into a one line arrow function
const isYesNo = (answer) => answer.toLowerCase() === "yes" || answer.toLowerCase() === "no";
/*************** Get Animal By Name ***************/
// The original function
function getAnimalByName(name, animals) {
return animals.find(function(animal) {
return animal.name.toLowerCase() === name.toLowerCase();
});
}
// Converted into an arrow function on multiple lines
const getAnimalByName = (name, animals) => {
const doesNameMatch = (a) => a.name.toLowerCase() === name.toLowerCase()
animals.find( doesNameMatch );
}
// Advanced: Converted into a one line arrow function
const getAnimalByName = (name, animals) => animals.find( (a) => a.name.toLowerCase() === name.toLowerCase() );
// My version of what's happening inside the array .find() method
// The actual find method does not have the array parameter!
const myFind = (array, callback) => {
for(let item of array) {
const isCorrectOne = callback(item);
if(isCorrectOne) {
return item;
}
}
}
// My version of what's happening inside the array .map() method
// The actual map method does not have the array parameter!
const myMap = (array, callback) => {
const newArray = [];
for(let item of array) {
const newItem = callback(item);
newArray.push(newItem);
}
return newArray;
}
// My version of what's happening inside the array .filter() method
// The actual filter method does not have the array parameter!
const myFilter = (array, callback) => {
const filteredArray = [];
for(let item of array) {
const shouldKeep = callback(item);
if(shouldKeep) {
filteredArray.push(item);
}
}
return filteredArray;
}
// My version of what's happening inside the array .reduce() method
// The actual reduce method does not have the array parameter!
const myReduce = (array, callback, initialValue) => {
let accumulator = initialValue;
for(let item of array) {
accumulator = callback(accumulator, item);
}
return accumulator;
}
// Using reduce to get the highest number in an array
let numbers = [12, 3, 15, 7, 20, 3];
const highest = numbers.reduce( (currentHighest, number) => {
if(number > currentHighest) {
return number;
}
return currentHighest;
}, 0);
/* PURELY FOR TEACHING PURPOSES */
/* This is what it would look like if we used my version of reduce instead */
/* There's no reason to ever use my version */
// const highest = myReduce(numbers, (currentHighest, number) => {
// if(number > currentHighest) {
// return number;
// }
// return currentHighest;
// }, 0);
alert(highest);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment