- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayQuiz-answer.js
More file actions
Latest commit
105 lines (93 loc) · 2.67 KB
/
Copy patharrayQuiz-answer.js
File metadata and controls
105 lines (93 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
console.clear();
// Q1. make a string out of an array
{
constfruits=['apple','banana','orange'];
constresult=fruits.join();
console.log(result);
}
// Q2. make an array out of a string
{
constfruits='🍎, 🥝, 🍌, 🍒';
constresult=fruits.split(',');
console.log(result);
}
// Q3. make this array look like this: [5, 4, 3, 2, 1]
{
constarray=[1,2,3,4,5];
constresult=array.reverse();// 배열 자체를 reverse함(array도)
console.log(result);
console.log(array);
}
// Q4. make new array without the first two elements
{
constarray=[1,2,3,4,5];
constresult=array.slice(2,5);// splice -> 배열 자체를 수정, slice는 end 값은 배제됨으로 4가 아닌 5로 설정해야 함
}
classStudent{
constructor(name,age,enrolled,score){
this.name=name;
this.age=age;
this.enrolled=enrolled;
this.score=score;
}
}
conststudents=[
newStudent('A',29,true,45),
newStudent('B',28,false,80),
newStudent('C',30,true,90),
newStudent('D',40,false,66),
newStudent('E',18,true,88),
];
// Q5. find a student with the score 90
{
constresult=students.find(function(student){
// console.log(student);
returnstudent.score===90;// true or false로 리턴
});
console.log(result);
}
// Q6. make an array of enrolled students(수업에 등록한 학생만!)
{
constresult=students.filter((student)=>student.enrolled);
console.log(result);
}
// Q7. make an array containing only the students' scores
// result should be: [45, 80, 90, 66, 88]
{
constresult=students.map((student)=>student.score);
console.log(result)
}
// Q8. check if there is a student with the score lower than 50
{
constresult=students.some((student)=>student.score<50);
console.log(result);
constresult1=students.every((student)=>student.score<50);// 모든 배열이 조건을 만족할 때만 true가 나옴; 모든 학생들이 50점 미만일 때만 true
console.log(result1);
}
// Q9. compute students' average score
{
constresult=students.reduce((prev,curr)=>{// reduceRight; 뒤에서부터 시작
console.log('------');
console.log(prev);
console.log(curr);
returnprev+curr.score;
},0);
console.log(result/students.length);
}
// Q10. make a string containing all the scores
// result should be: '45, 80, 90, 66, 88'
{
constresult=students
.map((student)=>student.score)
.join();
console.log(result);
}
// Bonus! do Q10 sorted in ascending order
// result should be: '45, 66, 80, 88, 90'
{
constresult=students
.map((student)=>student.score)
.sort((a,b)=>a-b)
.join()
console.log(result);
}