λ°°μ΄ 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 ~)