Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created March 29, 2020 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/bb57dcf8256137e6d9840ce9f1bcd1ae to your computer and use it in GitHub Desktop.
Save codecademydev/bb57dcf8256137e6d9840ce9f1bcd1ae to your computer and use it in GitHub Desktop.
Codecademy export
//challenge 1: define a callback function before you use it an iterator
const capitals = [ 'Amsterdam', 'London', 'Brussels', 'Oslo', 'Stockholm', 'Paris', 'Ankara', 'Alberta'];
const cityA = cityA => { //callback function
return cityA[0] === 'A';
};
const startsWithA=capitals.filter(cityA); //iterator
console.log(startsWithA);
//challenge 2: chain two iteration methods
const shortCity=startsWithA.some(city => city.length < 7);
//challenge 3, use optional arguments in an iterator
console.log(capitals.indexOf('Brussels', 0)); //should return 2
// challenge 4: return a single array from a multilayer array
const numbers = [[1,2],[3,4],[5,6],[7,8]];
let newArray = numbers.reduce(function(accumulator, currentValue) {
return accumulator.concat(currentValue);
},
[]
);
console.log(newArray);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment