(드림코딩) Array APIs 문제풀어보기

정세비·2021년 5월 20일
0

test

목록 보기
8/13
post-thumbnail

Q1. make a string out of an array

const fruits = ['apple', 'banana', 'orange'];

<정답>
const newFruits = fruits.join();

//output: apple,banana,orange

Q2. make an array out of a string

const fruits = '🍎, 🥝, 🍌, 🍒';

<정답>
const result = fruits.split(',')

//output:  ["🍎", " 🥝", " 🍌", " 🍒"]

Q3. make this array look like this: [5, 4, 3, 2, 1]

const array = [1, 2, 3, 4, 5];

<정답>
const result = array.reverse();

//output: [5, 4, 3, 2, 1]

Q4. make new array without the first two elements

const array = [1, 2, 3, 4, 5];

<정답>
const result = array.slice(2, 5);

//output: [3, 4, 5]

.slice(start index, end index+1)

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 result = students.find(function(value) {
  return value.score === 90;
});

또는

const result = students.find((value) => value.score === 90);

Q.6 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 result = students.filter((value) => value.enrolled === ture);

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 result = students.map((value) => value.score);

//output: [45, 80, 90, 66, 88]

Q8. check if there is a student with the score lower than 50

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 result = students.some((value) => value.score < 50);

//output: true   (50점 미만인 사람이 1명 있음)

만약 모든 요소들이 조건을 충족해야 true가 되게 하고싶다면 every를 사용하면 됨

const result = students.every((value) => value.score < 50);
//output: false

따라서 every를 이용해서 답을 도출하고 싶다면 !를 써서
const result = !students.every((value) => value.score >= 50); 

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),
];


<정답>
const result = students.reduce((prev, curr) => prev + curr.score, 0);
console.log(result / students.length);

//output: 73.8

reduce는 callback 함수나 initialvalue를 전달할수 있음
callback함수는 배열안에 있는 모든 요소들마다 호출이 됨(filter나 some, every와 비슷)
return되는 값은 누적된 결과값을 return함

즉, reduce는 배열에 있는 모든 요소들의 값을 누적하는, 무언가 함께 모아놓을 때 쓰는구나..라고 생각하면 됨.

const result = students.reduce((prev, curr) => {
console.log(prev);
console.log(curr);
return prev + curr.score;
}, 0);

에서
prev value는 이 전에 callback 함수에서 return된 값이 전달되고
curr은 배열의 아이템을 순차적으로 전달받는 것.

Q10. make a string containing all the scores

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 result = students.map((value) => value.score).join();

Q10-2. 점수가 50점 이상인 아이들만 뽑기

const result = students.map((value) => value.score).filter((value2) => value2 >= 50)
  console.log(result);
//output : [80, 90, 66, 88]


좀 더 가독성을 높여서 작성하면

const result = students
.map((value) => value.score)
.filter((value2) => value2 >= 50)
 
console.log(result);

// output: [80, 90, 66, 88]

Q11. do Q10 sorted in ascending order (result should be: '45, 66, 80, 88, 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 result = students
  .map((value) => value.score)
  .sort((a, b) => a - b)
  .join();
  console.log(result);


만약 큰 수-> 작은 수 순으로 정렬하고 싶다면 a-b를 b-a로 작성하면 됨

const result = students
.map((value) => value.score)
.sort((a, b) => b - a)
.join();

으로 작성하면 됨
profile
파주

0개의 댓글