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 },
{ first: 'Marie', last: 'Curie', year: 1867, passed: 1934 },
{ first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630 },
{ first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543 },
{ first: 'Max', last: 'Planck', year: 1858, passed: 1947 },
{ first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979 },
{ first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852 },
{ first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905 },
{ first: 'Lise', last: 'Meitner', year: 1878, passed: 1968 },
{ first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 }
];
const people = [
'Bernhard, Sandra', 'Bethea, Erin', 'Becker, Carl', 'Bentsen, Lloyd', 'Beckett, Samuel', 'Blake, William', 'Berger, Ric', 'Beddoes, Mick', 'Beethoven, Ludwig',
'Belloc, Hilaire', 'Begin, Menachem', 'Bellow, Saul', 'Benchley, Robert', 'Blair, Robert', 'Benenson, Peter', 'Benjamin, Walter', 'Berlin, Irving',
'Benn, Tony', 'Benson, Leana', 'Bent, Silas', 'Berle, Milton', 'Berry, Halle', 'Biko, Steve', 'Beck, Glenn', 'Bergman, Ingmar', 'Black, Elk', 'Berio, Luciano',
'Berne, Eric', 'Berra, Yogi', 'Berry, Wendell', 'Bevan, Aneurin', 'Ben-Gurion, David', 'Bevel, Ken', 'Biden, Joseph', 'Bennington, Chester', 'Bierce, Ambrose',
'Billings, Josh', 'Birrell, Augustine', 'Blair, Tony', 'Beecher, Henry', 'Biondo, Frank'
];
const answer1 = inventors.filter((inventor) => {
return (1500 <= inventor.year && inventor.year < 1600) ? true : false;
})
/*0: {first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642}
1: {first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630}*/
const answer2 = inventors.map((inventor) => {
return inventor.first + ' ' + inventor.last
})
/*['Albert Einstein', 'Isaac Newton', 'Galileo Galilei', 'Marie Curie', 'Johannes Kepler', 'Nicolaus Copernicus', 'Max Planck', 'Katherine Blodgett', 'Ada Lovelace', 'Sarah E. Goode', 'Lise Meitner', 'Hanna Hammarström']*/
create a list of Boulevards in Paris that contain 'de' anywhere in the name
프랑스 파리의 도로명 처럼 '~의 도로' Boulevards de ~
를 만들어주면 된다고 생각했다.
const answer6 = inventors.map((inventor) => {
return `Boulevard de ${inventor.first} ${inventor.last}`
})
/*
['Boulevard de Ada Lovelace', 'Boulevard de Sarah E. Goode', 'Boulevard de Johannes Kepler', 'Boulevard de Marie Curie', 'Boulevard de Nicolaus Copernicus', 'Boulevard de Albert Einstein', 'Boulevard de Galileo Galilei', 'Boulevard de Hanna Hammarström', 'Boulevard de Katherine Blodgett', 'Boulevard de Isaac Newton', 'Boulevard de Max Planck', 'Boulevard de Lise Meitner']*/
사실은 urlhttps://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
에 나와있는 도로들 중 de가 들어있는 것들만 리턴해야했다.
const answer6 = inventors.map((inventor) => {
return `Boulevard de ${inventor.first} ${inventor.last}`
})
console.log(answer6)
const category = document.querySeletor('.mw-category');
// const links = category.querySelectorAll('a');
//const de = links.map(link => link.textContent);
//이렇게 하면 작동하지 않는다. querySelector로 선택해 NodeList가 반환되므로 배열로 바꾸어줘야 한다.
const links = Array.from(category.querySelectorAll('a'));//전개연산자도 사용가능
const de = links
.map(link => link.textContent)
.filter(streetName => streetName.includes('de'));
const answer3 = inventors.sort((a, b) => {
return a.year - b.year;
}
)
/*0: {first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979}
1: {first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543}
2: {first: 'Marie', last: 'Curie', year: 1867, passed: 1934}
3: {first: 'Albert', last: 'Einstein', year: 1879, passed: 1955}
4: {first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642}
5: {first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905}
6: {first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909}
7: {first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630}
8: {first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852}
9: {first: 'Lise', last: 'Meitner', year: 1878, passed: 1968}
10: {first: 'Isaac', last: 'Newton', year: 1643, passed: 1727}
11: {first: 'Max', last: 'Planck', year: 1858, passed: 1947}*/
const answer5 = inventors.sort((a, b) => {
return (a.passed - a.year) - (b.passed - b.year);
})
/*
0: {first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979}
1: {first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543}
2: {first: 'Marie', last: 'Curie', year: 1867, passed: 1934}
3: {first: 'Albert', last: 'Einstein', year: 1879, passed: 1955}
4: {first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642}
5: {first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905}
6: {first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909}
7: {first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630}
8: {first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852}
9: {first: 'Lise', last: 'Meitner', year: 1878, passed: 1968}
10: {first: 'Isaac', last: 'Newton', year: 1643, passed: 1727}
11: {first: 'Max', last: 'Planck', year: 1858, passed: 1947}*/
The localeCompare() 메서드는 기준 문자열과 비교했을 때
비교 대상 문자열이 정렬상 전에 오는지, 후에 오는지 혹은 같은 순서에 배치되는지를 알려주는 숫자를 리턴한다.
Sort the people alphabetically by last name
const answer7 = inventors.sort((a, b) => {
return a.last.localeCompare(b.last)
})
/*
0: {first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979}
1: {first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543}
2: {first: 'Marie', last: 'Curie', year: 1867, passed: 1934}
3: {first: 'Albert', last: 'Einstein', year: 1879, passed: 1955}
4: {first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642}
5: {first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905}
6: {first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909}
7: {first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630}
8: {first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852}
9: {first: 'Lise', last: 'Meitner', year: 1878, passed: 1968}
10: {first: 'Isaac', last: 'Newton', year: 1643, passed: 1727}
11: {first: 'Max', last: 'Planck', year: 1858, passed: 1947}
*/
이렇게 하면 curr의 데이터 타입이 number여도 누산기에는 제대로 합계를 구할 수 없다. 아마도 원본이 객체이기 때문인것 같다.
const answer4 = inventors.reduce((sum, curr) => {
const currYear = (curr.passed - curr.year)
return sum + currYear;
})
[object Object]78
index-START.html:78 [object Object]7859
index-START.html:78 [object Object]785984
index-START.html:78 [object Object]78598437
index-START.html:78 [object Object]7859843780
index-START.html:78 [object Object]785984378050
index-START.html:78 [object Object]78598437805089
index-START.html:78 [object Object]7859843780508967
index-START.html:78 [object Object]785984378050896790
index-START.html:78 [object Object]78598437805089679076
index-START.html:81 [object Object]7859843780508967907681
초기값을 0 으로 주면 해결된다.
0에서 시작해 '숫자'를 더할 수 있다.
const answer4 = inventors.reduce((sum, curr) => {
return sum+ (curr.passed - curr.year)
},0)
//861
다른 풀이로도 풀어봤다.
원본 객체에서 필요한 숫자 - 살아온 기간만 리턴해 배열을 만들어주고, 그 배열에서 reduce로 합계를 구하면 된다.
const yearArr = inventors.map((inventor) => {
return inventor.passed - inventor.year
})
//[70, 78, 59, 84, 37, 80, 50, 89, 67, 90, 76, 81]
const answer4 = yearArr.reduce((sum, curr) => {
return sum + curr;
})
//861
// 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 answer8 = data.reduce((sum, curr) => {
return sum.includes(curr) ? sum : [...sum,curr];
}, [])
//['car', 'truck', 'bike', 'walk', 'van']
const transportation = data.reduce(function(obj, item) {
if(!obj[item]) {
obj[item] = 0;
}
obj[item]++;
return obj;
}, {});
/*
bike: 2
car: 5
truck:3
van: 2
walk: 2
*/