//Q1. make a string out of an array
{
const fruits = ['apple','banana','orange'];
let result = "";
for(fruit of fruits){
result = result+fruit;
}
console.log(result);
}
//Q1. 답
{
const fruits = ['apple','banana','orange'];
const result = fruits.join('6'); //<< 배열에 join을 사용하면 배열을 다 합쳐서 string으로 만들어줌 (구분자 지정도 가능)
console.log(result);
}
//Q2. 스트링을 배열로 바꿔라
{
const fruits = "사과,키위,바나나,체리";
const result = fruits.split(",");
console.log(result);
}
//Q2. 답 (split)
{
const fruits = "사과,키위,바나나,체리";
const result = fruits.split(",");
console.log(result);
}
//Q3. 배열을 거꾸로 만들어라
{
const array = [1,2,3,4,5];
const result = array.reverse();
console.log(result);
}
//Q3. 답 (reverse)
{
const array = [1,2,3,4,5];
const result = array.reverse(); //<<==배열 자체도 반대로 정렬되어서 이 이후로는 array도 반대정렬모습으로 출력됨.
console.log(result);
console.log(array);
}
console.clear();
//Q4. 5개의 요소중에서 뒤 3개의 요소로만 배열을 만들어라
{
const array = [1,2,3,4,5];
const result = array.slice(2); //슬라이스는 배열을 수정하지 않고 새로운값만 return해줌
console.log(result);
}
//Q4. 답 slice
{
const array = [1,2,3,4,5];
const result = array.splice(0,2); //배열에서 인덱스0부터 2개를 삭제하고 그 삭제한것을 return으로 보내줌
console.log(result);
console.log(array); //배열에선 1과 2가 삭제되었음.
}
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.점수가 90인 학생을 찾아라
{
let result;
for(student of students){
if(student.score === 90){
result=student
break;
}
}
console.log(result);
}
console.clear();
//Q5.답 (find)
{
const result = students.find((student,index)=> student.score === 90 );
//배열안에있는 index를 돌면서 각 index마다 callback함수를 호출한다
//(callback함수는 Boolean형을 return하고 true를 리턴하면 stop한다.)
//멈추면 그때의 배열값을 return해준다.
console.log(result);
}
//Q6. 수업에 등록한 학생들만 골라서 배열을 만들어라
{
let result=[];
for(student of students){
if(student.enrolled){
result.push(student)
}
}
console.log(result);
}
//Q6. 답 (filter)
{
const result = students.filter((student)=> student.enrolled); // filter(callback함수) 는 callback함수가 true인 아이들만 모아서 배열로 만들어준다.
console.log(result);
}
//Q7. 학생들 배열에서 점수만 뽑아서 점수만 들어있는 배열을 만든다.
{
let result = [];
for(student of students) result.push(student.score);
console.log(result);
}
//Q7. 답 (map())
{
const result = students.map((student)=> student.score) //map안의 콜백함수와 배열의 인자가 맵핑되어 콜백함수의 return으로 바뀜
console.log(result); //콜백함수의 인자는 최대한 이해하기 쉽도록 정한다
}
//Q8. 배열안에 학생들 점수가 50점보다 낮은애들이 있는지 확인하라
{
const result = students.filter((student)=>student.score<50);
console.log(result);
}
//Q8. 답 (some()) //some안의 callback함수의 조건이 맞는것이 하나라도 있으면 true / 없으면 false를 return하는 함수
{
console.clear();
const result = students.some((student)=>student.score<50);
console.log(result);
const result2 = students.every((student)=>student.score>=50); // 배열 안의 모든 요소들이 callback함수의 조건을 만족하면 true를 리턴
console.log(result2);
}
//Q9. 학생들의 평균점수를 구해라
{
let total=0;
for(student of students) total +=student.score;
let avg= total/students.length;
console.log(avg);
}
//Q9. 답 (reduce())
{
const result = students.reduce((prev,curr)=>{ //배열안의 모든 요소를 부른다 , 그리고 배열안의 모든 요소들을 누적시킨다. 콜백함수는 return값이 있어야한다.
console.log('--------------'); //return으로 보낸 값을 다음 반복에서 prev로 두고 curr은 다음 배열의 요소를 가져오는방식
console.log(prev);
console.log(curr);
return prev + curr.score;
}, 0);
console.log(result/students.length);
}
//Q10. 학생들의 모든 점수를 String으로 바꿔서 나타내라
{
const result = students.reduce((prev,curr)=>prev+curr.score+", ","");
console.log(result);
}
//Q10.답
{
const result = students.map(student => student.score)
.filter(score=> score>=50)
.join(",");
console.log(result);
}
//보너스 Q10. 학생들의 점수를 낮은점수부터 정렬하게 해서 String으로 출력해보세요
const result = students.map((student)=>student.score)
.sort()
.join(",");
console.log(result);
잼땅
'JS > 드림코딩앨리' 카테고리의 다른 글
11. 비동기 처리의 시작 콜백 이해하기, 콜백 지옥 체험 (0) | 2021.04.21 |
---|---|
10. JSON 개념 정리 와 활용방법 (0) | 2021.04.21 |
8. 배열 제대로 알고 쓰자. 자바스크립트 배열 개념과 APIs 총정리 (0) | 2021.04.21 |
7. Object 넌 뭐니? (0) | 2021.04.21 |
6. 클래스와 오브젝트의 차이점(class vs object), 객체지향 언어 클래스 정리 (0) | 2021.04.21 |