[Java Script 개념]

Java Script Array API이용한 퀴즈

ho-bolt 2021. 8. 22. 22:39

 

1.  Q1. make a string out of an array

=> apple,banana,orange

:join api는 배열의 모든 요소를 하나의 문자열로 만들어주는 api이다. 

join()안에는 원하는 구분자를 넣어주면 된다. 

 

==============================================================

2.  Q2. make an array out of a string

 

=>["🍎, 🥝, 🍌, 🍒"]

: split메서드는 구분자를 이용하여, 하나의 string객체를 여러개의 문자열로 나눈다. 

 

split(구분자, limit)

만약 구분자를 넣지 않는다면 하나의 문자열로 정의되기 때문에 꼭 넣어주는 것이 좋다. 

limit은 옵션인데 배열의 원소가 limit의 개수가 되면 멈춘다. 

 

==============================================================

 

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

=>  [5,4,3,2,1]

:reverse메소드는 배열의 원소 순서를 거꾸로 만들어 줍니다. 

 

==============================================================

 

4.  Q4. make new array without the first two elements

=> [3,4,5]

: slice메소드는 배열의 시작과 끝을 인자값으로 넣어주면 그 범위만큼 값들을 추출한다. 

slice(begin,[end]) 

여기서 begin은 index를 의미하기 때문에 0부터 시작한 것을 의미합니다. 만약 end에 음수가 들어간다면, 배열의 끝에서부터의 출발합니다. end를 전달하지 않으면 배열의 전체길이를 출력합니다.  

 

==============================================================

 

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

 

 

=>Student {name: "C", age: 30, enrolled: true, score: 90}

:find메소드는 배열에서 주어진 callback함수를 만족하는 첫 번째 값을 반환합니다. 

callback함수는 boolean타입으로 true,false를 리턴합니다. 인자값으로 element, index, array ,this를 넣어 전달할 수 있다. students 배열을 돌면서, 90점 이상이 아닌 학생은 FALSE를 리턴하고 90이상인 학생이 나오면 TRUE를 리턴하고 끝낸다. 

하지만 화살함수를 이용하면 return과 function을 생략하여 표현할 수 있다. 

 

==============================================================

 

6. Q6. make an array of enrolled students

 

=>

  1. 0: Student {name: "A", age: 29, enrolled: true, score: 45}
  2. 1: Student {name: "C", age: 30, enrolled: true, score: 90}
  3. 2: Student {name: "E", age: 18, enrolled: true, score: 88}

: filter  

find의 경우 함수 테스트를 통과한 한 개의 값만 리턴한 반면,

filter는 주어진 함수 테스트를 통과한 배열의 모든 요소를 모아서 새로운 배열을 만든다.

 

==============================================================

7. Q7. make an array containing only the students' scores

=>[45, 80, 90, 66, 88]

:map 메소드는 설정한 함수의 결과를 모아서 새로운 배열을 만드는 메소드이다. 

구문 : arr.map(callback(currentValue[, index[, array]])[, thisArg]) 

 

==============================================================

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

 

=>false

: every 메소드는 배열안의 모든 요소가 주어진 함수 테스트를 통과하는 지를 테스트합니다. 

boolean타입으로 true,false를 반환합니다. 

하나라도 주어진 테스트를 만족하지 못하면 false를 리턴합니다. 

every(element,index,array)등 을 인자값으로 받을 수 있습니다. 

 

==============================================================

9. Q9. compute students' average score

 

=>73.8

 

:reduce 메소드는 원하는 시작점부터 모든 배열을 돌면서 값을 누적할 때 사용한다. 

배열을 돌 때 하나씩 curr에 값을 전달해준다. 

prev는 현재 curr값을 리턴할 때 다음에 순서에 prev로 간다.

이것이 반복되면서 누산이 가능해지는 것이다. 

 

구문 : arr.reduce(callback[, initialValue])

 

==============================================================

10. Q10. make a string containing all the scores and over 50 

=>80,90,66,88

이렇게 여러가지 메소드를 섞어서 사용할 수 있다.

 

==============================================================

11. do Q10 sorted in descending order

=>90,88,80,66,45

:sort메소드는 배열을 정렬하는 함수이다. a,b,2가지 값을 받아서 만약 0보다 작다면 a를 작은 index로 옮기고

b가 작다면 b를 작은 index로 옮긴다. 

 

 

# 이 퀴즈는 유튜버 드림코딩 님의 퀴즈를 풀어본 것입니다. 메소드 설명은 MDN페이지를 참고 했습니다. 

728x90