๐Ÿ“ ์˜ค๋Š˜ ํ•œ ๊ฒƒ

  1. ๋ฐฐ์—ด APIs ํ€ด์ฆˆ ํ’€์ด & ์˜ค๋‹ต ๋ถ„์„

  2. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ•์˜ ์ˆ˜๊ฐ• (๋ฐฐ์—ด APIs)

  3. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด


๐Ÿ“– ํ•™์Šต ์ž๋ฃŒ

  1. ๋“œ๋ฆผ์ฝ”๋”ฉ ์œ ํŠœ๋ธŒ '์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ' ๊ฐ•์˜ 9ํŽธ

  2. ์‚ฌ์ „ ํ•™์Šต ๊ฐ€์ด๋“œ STEP 2 (Quiz), STEP 3 (Algorithm), How to tackle problems


๐Ÿ“š ๋ฐฐ์šด ๊ฒƒ

๋ฐฐ์—ด ํ•จ์ˆ˜๋“ค(Array APIs)


1. ๋“œ๋ฆผ์ฝ”๋”ฉ ์œ ํŠœ๋ธŒ ๊ฐ•์˜ 9ํŽธ (ํ€ด์ฆˆ 11๋ฌธ์ œ)

๐Ÿ“Œ 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);
}

์•Œ๊ณ ๋ฆฌ์ฆ˜(Argorithm)


1. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ฌธ์ œ ํ’€์ด

์ฒซ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธํ’€ ๐Ÿ™ƒ

1) ์„œ์šธ์—์„œ ๊น€์„œ๋ฐฉ ์ฐพ๊ธฐ

๐Ÿ”Ž ์ตœ์ข… ํ’€์ด

function solution(seoul) {
  var answer = `๊น€์„œ๋ฐฉ์€ ${seoul.indexOf('Kim')}์— ์žˆ๋‹ค`;
  return answer;
}

2) ๊ฐ€์šด๋ฐ ๊ธ€์ž ๊ฐ€์ ธ์˜ค๊ธฐ

๐Ÿ”Ž ์ตœ์ข… ํ’€์ด

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);
}

3) ์ˆ˜๋ฐ•์ˆ˜๋ฐ•์ˆ˜๋ฐ•์ˆ˜๋ฐ•์ˆ˜

๐Ÿ”Ž ์ตœ์ข… ํ’€์ด

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)์ด๋ผ๊ณ  ์“ฐ๋ฉด ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•ด์ฃผ๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค.


โœจ ๋‚ด์ผ ํ•  ๊ฒƒ

  1. ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€ ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ธฐ

  2. ์‚ฌ์ „ ํ•™์Šต ๊ฐ€์ด๋“œ STEP 4 (DOM ~)

profile
dev log

0๊ฐœ์˜ ๋Œ“๊ธ€

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด

Powered by GraphCDN, the GraphQL CDN