Skip to content

Commit

Permalink
Add day 4
Browse files Browse the repository at this point in the history
  • Loading branch information
sachincool committed Jan 4, 2018
1 parent 6d03fd1 commit 6443164
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions 04 - Array Cardio Day 1/index-START.html
Expand Up @@ -11,8 +11,7 @@
// ## Array Cardio Day 1

// Some data we can work with

const inventors = [
const inventors = [
{ first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
{ first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 },
{ first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 },
Expand All @@ -31,29 +30,66 @@

// Array.prototype.filter()
// 1. Filter the list of inventors for those who were born in the 1500's
const fifteen=inventors.filter(inventor => inventor.year>=1500 && inventor.year<=1600);
// console.table(fifteen);

// Array.prototype.map()
// 2. Give us an array of the inventors' first and last names
const fullNames=inventors.map(inventor=>`${inventor.first} ${inventor.last}`);
// console.log(fullNames);

// Array.prototype.sort()
// 3. Sort the inventors by birthdate, oldest to youngest
const ordered = inventors.sort((a,b)=>a.year>=b.year?1:-1);
// console.table(ordered);


// Array.prototype.reduce()
// 4. How many years did all the inventors live?

// 5. Sort the inventors by years lived
const totalYears =inventors.reduce((total,inventor)=>{return total +(inventor.passed-inventor.year)},0);
// console.log(totalYears);


// 5. Sort the inventors by years lived
const oldest=inventors.sort(function(a,b){
const LastGuy=a.passed-a.year;
const NextGuy= b.passed-b.year;
return LastGuy>NextGuy? -1:1;
});
// console.table(oldest);
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris


//const links=Array.from(document.querySelectorAll('.mw-category a'));
//const de= links.map(link=>link.textContent).filter(streetName=>streetName.include('de'));
// console.log(de);
// 7. sort Exercise
// Sort the people alphabetically by last name
const alpha=people.sort((lastOne,nextOne)=>{
const [alast,afirst]=lastOne.split(', ');
const [blast,bfirst]=nextOne.split(', ');
return alast>blast ?1:-1;


});
console.log(alpha);


// 8. Reduce Exercise
// Sum up the instances of each of these

const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];

const transportation=data.reduce((obj,item)=>{
if(!obj[item])obj[item]=0;

obj[item]++;
return obj;

},{});
console.log(transportation);

</script>
</body>
</html>

0 comments on commit 6443164

Please sign in to comment.