๋ฐฐ์ด APIs ํด์ฆ ํ์ด & ์ค๋ต ๋ถ์
์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ ์๊ฐ (๋ฐฐ์ด APIs)
ํ๋ก๊ทธ๋๋จธ์ค ์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ํ์ด
๋๋ฆผ์ฝ๋ฉ ์ ํ๋ธ '์๋ฐ์คํฌ๋ฆฝํธ' ๊ฐ์ 9ํธ
์ฌ์ ํ์ต ๊ฐ์ด๋ STEP 2 (Quiz), STEP 3 (Algorithm), How to tackle problems
๐ 9ํธ ๊ฐ์์์ ๋ค๋ฃจ๋ ๋ฉ์๋ :
join() / split() โ ๋ฌธ์์ด ๋ฉ์๋ / reverse() / splice() / slice() /
find() / filter() / map() / some() / reduce() / sort()
Q1. Make a string out of an array.
{ const fruits = ['apple', 'banana', 'orange']; }
๐ ๋ด ๋ต๋ณ (ok)
{ const fruits = ['apple', 'banana', 'orange']; const makeString = fruits.toString(); // 'apple,banana,orange' โ toString ๋์ join ์ฌ์ฉ ๊ฐ๋ฅ console.log(makeString); }
Q2. Make an array out of a string.
{ const fruits = '๐, ๐ฅ, ๐, ๐'; }
๐ ๋ด ๋ต๋ณ (ok)
{ const fruits = '๐, ๐ฅ, ๐, ๐'; const makeArray = fruits.split(', '); // ['๐', '๐ฅ', '๐', '๐'] // split()๋ ๋ฌธ์์ด ๋ฉ์๋์ด๋ค console.log(makeArray); }
Q3. Make this array look like this: [5, 4, 3, 2, 1]
{ const array = [1, 2, 3, 4, 5]; }
๐ ๋ด ๋ต๋ณ (ok)
{ const array = [1, 2, 3, 4, 5]; const reverseArray = array.reverse(); console.log(reverseArray); }
Q4. Make new array without the first two elements.
{ const array = [1, 2, 3, 4, 5]; }
๐ ๋ด ๋ต๋ณ
{ const array = [1, 2, 3, 4, 5]; array.shift(); array.shift(); const useShift = array; console.log(useShift); }
๐ ์๋ฆฌ ๋ต๋ณ
{ const array = [1, 2, 3, 4, 5]; const newArray = array.slice(2, 5); console.log(newArray); } // shift()์ splice()๋ ๊ธฐ์กด array ์์ฒด๋ฅผ ๋ณํ์ํจ๋ค // ๊ทธ๋ฌ๋ ๋ฌธ์ ๋ ๊ธฐ์กด array๋ฅผ ๋ณํ์ํค๋ ๊ฒ ์๋๋ผ ์๋ก์ด ๋ฐฐ์ด์ ๋ง๋๋ ๊ฒ์ด๋ค // ๋ฐ๋ผ์ slice()๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค
๐ก 5 ~ 10๋ฒ ๋ฌธ์ ๊ณตํต ์ฝ๋
๐ก ์ฌ๊ธฐ์ students๋๊ฐ์ฒด ๋ฐฐ์ด
์ด๋คclass Student { constructor(name, age, enrolled, score) { this.name = name<; this.age = age; this.enrolled = enrolled; this.score = score; } } const students = [ new Student('A', 29, true, 45), new Student('B', 28, false, 80), new Student('C', 30, true, 90), new Student('D', 40, false, 66), new Student('E', 18, true, 88), ];
Q5. Find a student with the score 90.
๐ ๋ด ๋ต๋ณ
{ class Student { constructor(name, age, enrolled, score) { this.name = name; this.age = age; this.enrolled = enrolled; this.score = score; } } const students = [ new Student('A', 29, true, 45), new Student('B', 28, false, 80), new Student('C', 30, true, 90), new Student('D', 40, false, 66), new Student('E', 18, true, 88), ]; const find90 = students.find((Student) => Student.score = 90); console.log(find90); }
๐ ์๋ฆฌ ๋ต๋ณ
{ class Student { constructor(name, age, enrolled, score) { this.name = name; this.age = age; this.enrolled = enrolled; this.score = score; } } const students = [ new Student('A', 29, true, 45), new Student('B', 28, false, 80), new Student('C', 30, true, 90), new Student('D', 40, false, 66), new Student('E', 18, true, 88), ]; const with90 = students.find(function (student) { return student.score === 90; }) console.log(with90); /* ์ด๋ ์ฝ๋ฐฑ ํจ์์ ๋งค๊ฐ๋ณ์(student)๋ ๋ฐฐ์ด(students)์ ์์ ํ๋ํ๋ ์ฆ, ์ฌ๊ธฐ์๋ ๊ฐ๊ฐ์ ๊ฐ์ฒด{}๋ฅผ ๊ฐ๋ฆฌํจ๋ค students ๋ฐฐ์ด์ ์์ ํ๋ํ๋์ ์์ฐจ์ ์ผ๋ก ์ฝ๋ฐฑ ํจ์๊ฐ ํธ์ถ๋๋ค(looping) ์ฝ๋ฐฑ ํจ์๊ฐ true๊ฐ ๋ ๋, ์ฆ (student.score === 90)์ด true์ผ ๋ loop๋ฅผ ๋ฉ์ถ๊ณ , ๊ทธ ์์๋ฅผ ๋ฐํํ๋ค find() ๋ฉ์๋๋ฅผ ํ์ดํ ํจ์๋ก ๋ฐ๊ฟ ์ฐ๋ฉด, const with90 = students.find((student) => student.score === 90); */ }
Q6. Make an array of enrolled students.
๐ ๋ด ๋ต๋ณใ ใ ใ
{ class Student { constructor(name, age, enrolled, score) { this.name = name; this.age = age; this.enrolled = enrolled; this.score = score; } } const students = [ new Student('A', 29, true, 45), new Student('B', 28, false, 80), new Student('C', 30, true, 90), new Student('D', 40, false, 66), new Student('E', 18, true, 88), ]; const enrolled = students.filter((student) => student.enrolled = true); console.log(enrolled); }
๐ ์๋ฆฌ ๋ต๋ณ
{ class Student { constructor(name, age, enrolled, score) { this.name = name; this.age = age; this.enrolled = enrolled; this.score = score; } } const students = [ new Student('A', 29, true, 45), new Student('B', 28, false, 80), new Student('C', 30, true, 90), new Student('D', 40, false, 66), new Student('E', 18, true, 88), ]; const enrolled = students.filter((student) => student.enrolled === true); console.log(enrolled); } // =๊ฐ ๋ฌธ์ ์๋ค. =์ ===๋ฅผ ๊ตฌ๋ถํ์ โ
Q7. Make an array containing only the students' scores.
result should be: [45, 80, 90, 66, 88]๐ ๋ด ๋ต๋ณใ ใ ใ
{ class Student { constructor(name, age, enrolled, score) { this.name = name; this.age = age; this.enrolled = enrolled; this.score = score; } } const students = [ new Student('A', 29, true, 45), new Student('B', 28, false, 80), new Student('C', 30, true, 90), new Student('D', 40, false, 66), new Student('E', 18, true, 88), ]; const values = students.values(); for (let value of values) { console.log(value.score); } }
๐ ์๋ฆฌ ๋ต๋ณ
{ class Student { constructor(name, age, enrolled, score) { this.name = name; this.age = age; this.enrolled = enrolled; this.score = score; } } const students = [ new Student('A', 29, true, 45), new Student('B', 28, false, 80), new Student('C', 30, true, 90), new Student('D', 40, false, 66), new Student('E', 18, true, 88), ]; const scores = students.map((student) => student.score); console.log(scores); } // value()๊ฐ ์๋๋ผ map()์ ์ฌ์ฉํด์ผ ํ๋ค
Q8. Check if there is a student with the score lower than 50.
๐ ๋ด ๋ต๋ณ (ok)
{ class Student { constructor(name, age, enrolled, score) { this.name = name; this.age = age; this.enrolled = enrolled; this.score = score; } } const students = [ new Student('A', 29, true, 45), new Student('B', 28, false, 80), new Student('C', 30, true, 90), new Student('D', 40, false, 66), new Student('E', 18, true, 88), ]; const lower50 = students.some(Student => Student.score < 50); console.log(lower50); }
Q9. Compute students' average score.
๐ ๋ด ๋ต๋ณ
{ class Student { constructor(name, age, enrolled, score) { this.name = name; this.age = age; this.enrolled = enrolled; this.score = score; } } const students = [ new Student('A', 29, true, 45), new Student('B', 28, false, 80), new Student('C', 30, true, 90), new Student('D', 40, false, 66), new Student('E', 18, true, 88), ]; // score ๊ฐ๋ง์ผ๋ก ๊ตฌ์ฑ๋ ์๋ก์ด ๋ฐฐ์ด ๋ง๋ค๊ธฐ // ์ด๊ฑธ ์ด๋ป๊ฒ ํ๋์ง ๋ชจ๋ฅด๊ฒ ๋ค โ 8๋ฒ ๋ฌธ์ ๋ต๋ณ ์ฐธ๊ณ ! // ๊ทธ ๋ฐฐ์ด์ reduce ํจ์ ์ด์ฉํด์ ํฉ์ฐ ๊ตฌํ๊ธฐ // const addAllScores = scores.reduce((prev, curr) => prev + curr); // ๊ทธ ํฉ์ฐ์ 5๋ก ๋๋๊ธฐ // const average = addAllScores / 5; // console.log(average); }
๐ ์๋ฆฌ ๋ต๋ณ
{ class Student { constructor(name, age, enrolled, score) { this.name = name; this.age = age; this.enrolled = enrolled; this.score = score; } } const students = [ new Student('A', 29, true, 45), new Student('B', 28, false, 80), new Student('C', 30, true, 90), new Student('D', 40, false, 66), new Student('E', 18, true, 88), ]; // array.reduce() MDN '๊ฐ์ฒด ๋ฐฐ์ด์์์ ๊ฐ ํฉ์ฐ' ์ฐธ๊ณ const addALL = students.reduce((prev, curr) => prev + curr.score, 0); console.log(addALL / students.length); } /* ๋ด๊ฐ ์๊ฐํ ๋ฐฉ๋ฒ์ map()์ ํตํด score๋ก๋ง ๊ตฌ์ฑ๋ ๋ฐฐ์ด์ ๋ง๋ค๊ณ , ์ฌ๊ธฐ์ reduce() ์ฌ์ฉํ๋ ๊ฒ์ด์๋๋ฐ reduce()๋ง์ผ๋ก ๊ฐ๋ฅํ๋ค reduce()์ ๋ํ ์ดํด๋ ๋ถ์กฑํ๋ค ํ๊ท ์ ๊ตฌํ ๋ ๋ถ๋ชจ์๋ '๋ฐฐ์ด์ ๊ธธ์ด'๋ฅผ ๋ฃ์ด๋ผ */
Q10. Make a string containing all the scores.
result should be: '45, 80, 90, 66, 88'๐ 9๋ฒ ๋ฌธ์ ๊น์ง ๊ฐ์๋ฅผ ๋ฃ๊ณ ๋ ํ์ ๋ด ๋ต๋ณ
{ class Student { constructor(name, age, enrolled, score) { this.name = name; this.age = age; this.enrolled = enrolled; this.score = score; } } const students = [ new Student('A', 29, true, 45), new Student('B', 28, false, 80), new Student('C', 30, true, 90), new Student('D', 40, false, 66), new Student('E', 18, true, 88), ]; const scores = students.map((student) => student.score); const makeString = scores.join(', '); console.log(makeString); }
๐ ์๋ฆฌ ๋ต๋ณ
{ class Student { constructor(name, age, enrolled, score) { this.name = name; this.age = age; this.enrolled = enrolled; this.score = score; } } const students = [ new Student('A', 29, true, 45), new Student('B', 28, false, 80), new Student('C', 30, true, 90), new Student('D', 40, false, 66), new Student('E', 18, true, 88), ]; const scores = students .map((student) => student.score) .join(', '); // ํจ์ํ ํ๋ก๊ทธ๋๋ฐ // map()์ ๊ทธ ์์ฒด๋ก ๋ฐฐ์ด์ return ํ๊ธฐ ๋๋ฌธ์ // ๋ฐ๋ก ์ด์ด์ ๊ทธ ๋ฐฐ์ด์ api๋ฅผ ๋ ์ธ ์ ์๋ค console.log(scores); }
Q11. Do Q10 sorted in ascending order.
result should be: '45, 66, 80, 88, 90'๐ 10๋ฒ ๋ฌธ์ ๊น์ง ๊ฐ์ ๋ฃ๊ณ ๋ ํ์ ๋ด ๋ต๋ณ
{ class Student { constructor(name, age, enrolled, score) { this.name = name; this.age = age; this.enrolled = enrolled; this.score = score; } } const students = [ new Student('A', 29, true, 45), new Student('B', 28, false, 80), new Student('C', 30, true, 90), new Student('D', 40, false, 66), new Student('E', 18, true, 88), ]; const scores = students .map((student) => student.score) .sort(); console.log(scores); } // ๋ต์ ์ฐ์ฐํ ๊ฐ๊ฒ ๋์์ง๋ง // ๋ฐฐ์ด ์์ ์ค 400์ด ์์๋ค๋ฉด 45์ 66 ์ฌ์ด์ 400์ด ์์นํ๊ฒ ๋์ด // ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ์ด ์ ๋์์ ๊ฒ
๐ ์๋ฆฌ ๋ต๋ณ
{ class Student { constructor(name, age, enrolled, score) { this.name = name; this.age = age; this.enrolled = enrolled; this.score = score; } } const students = [ new Student('A', 29, true, 45), new Student('B', 28, false, 80), new Student('C', 30, true, 90), new Student('D', 40, false, 66), new Student('E', 18, true, 88), ]; const scores = students .map((student) => student.score) .sort((a, b) => a - b); // ์ค๋ฆ์ฐจ์ ์ ๋ ฌ console.log(scores); }
์ฒซ ์๊ณ ๋ฆฌ์ฆ ๋ฌธํ ๐
๐ ์ต์ข ํ์ด
function solution(seoul) { var answer = `๊น์๋ฐฉ์ ${seoul.indexOf('Kim')}์ ์๋ค`; return answer; }
๐ ์ต์ข ํ์ด
function solution(s) { if (s.length % 2 !== 0) { var answer = s[(s.length-1)/2]; } else { var answer = `${s[(s.length)/2 - 1]}${s[(s.length)/2]}`; } return answer; }
๐ ์ด๋ป๊ฒ ํ์๋
{ // ๊ฐ์ด๋ฐ ๊ธ์ ๊ฐ์ ธ์ค๊ธฐ const s = '12de56'; if (s.length % 2 !== 0) { var answer = s[(s.length-1)/2]; } else { var answer = `${s[(s.length)/2 - 1]}${s[(s.length)/2]}`; } console.log(answer); }
๐ ์ต์ข ํ์ด
function solution(n) { if (n % 2 === 0) { var answer2 = '์๋ฐ'.repeat(n/2); } else { var answer1 = '์๋ฐ'.repeat((n+1)/2); var answer2 = answer1.slice(0, -1); } return answer2; }
๐ ์ด๋ป๊ฒ ํ์๋
n = ์ง์ โ n/2 ํ ๊ฒ๋งํผ '์๋ฐ' ๋ฐ๋ณต
n = ํ์ โ n์ 1์ ๋ํ ๊ฒ๋งํผ '์๋ฐ' ๋ฐ๋ณตํ๊ณ ๋ง์ง๋ง ๋ฌธ์ ์ ๊ฑฐ{ // ์๋ฐ์๋ฐ์๋ฐ์๋ฐ์๋ฐ์ var n = 3; if (n % 2 === 0) { var answer2 = '์๋ฐ'.repeat(n/2); } else { var answer1 = '์๋ฐ'.repeat((n+1)/2); // n = 3, ์๋ฐ์๋ฐ var answer2 = answer1.slice(0, -1); // ์๋ฐ์ } console.log(answer2); }
๐ ๋ญ ์๋ก ๋ฐฐ์ ๋
string.slice()๋ ๋ฌธ์์ด์ ์์ ์ธ๋ฑ์ค๋ถํฐ ๋ ์ธ๋ฑ์ค '์ด์ '๊น์ง์ ์์๊ฐ์ ์๋ก์ด ๋ฌธ์์ด๋ก ๋ฐํํ๋ค. ๋ฐ๋ผ์, ์์ ํ์ด์ฒ๋ผslice(0, -1)
์ด๋ผ๊ณ ์ฐ๋ฉด๋ง์ง๋ง ๋ฌธ์๋ฅผ ์ ๊ฑฐ
ํด์ฃผ๋ ๊ฒ๊ณผ ๊ฐ๋ค.
์๊ณ ๋ฆฌ์ฆ ๊ณต๋ถ ์์ธํ ์์๋ณด๊ธฐ
์ฌ์ ํ์ต ๊ฐ์ด๋ STEP 4 (DOM ~)