.join()
괄호안에 넣은 문자로 구분자를 표시한다.// Q1 make a string out of an array
const fruits = ['apple', 'banana', 'orange'];
const result = fruits.join('|');
console.log(result); // apple | banana | orange
.split(',', number)
split은 꼭 구분자,
로 구분지어 줘야하고 뒤에 나오는 number(optional)는 몇개를 출력하는지를 나타낸다.// Q2 make an array out of a string
const fruits = '🍎, 🥝, 🍌, 🍒';
const result = fruits.split(',', 3);
console.log(result); // '🍎, 🥝, 🍌,`
.reverse()
// Q3 make this array look like this: [5, 4, 3, 2, 1]
const array = [1, 2, 3, 4, 5];
const result = array.reverse();
console.log(result);
console.log(array); // reverse를 사용하면 array 자체가 바뀌니 유의
.slice(number1, number2)
number1은 start index, number2는 end index, end index는 배제 되기 때문에 원하는값 +1// Q4 make new array without the first two elements
const array = [1, 2, 3, 4, 5];
const result = array.slice(2, 5);
console.log(array);
console.log(result);
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
{
console.clear();
const result = students.find(function (student) {
return student.score === 90;
});
console.log(result);
//Student {name: "C", age: 30, enrolled: true, score: 90}
}
// Q6. make an array of enrolled students
{
console.clear();
const result = students.find(function (student) {
return student.score === 90;
});
console.log(result); // Student A , Student C, Student E
}
// Q7. make an array containing only the students' scores
{
const result = students.map((student) => student.score);
console.log(result); // [45, 80, 90, 66, 88]
}
// Q8. check if there is a student with the score lower than 50
{
const result = students.every((student) => student.score >= 30);
console.log(result); // true
}
// Q9. compute students' average score
{
const result = students.reduce(function (prev, curr) {
return prev + curr.score;
}, 0);
console.log(result / 5); // 73.8
}
-apis 짬뽕가능, 묶어서 작성가능
// Q10. make a string containing all the scores
{
const result = students
.map((student) => student.score)
.filter((score) => score >= 50)
.join();
console.log(result);
}
-
값을 리턴하게 되면 a가 b보다 작다고 간주하게 되서 정렬이 된다.//Q11 sorted in ascending order
{
const result = students
.map((student) => student.score)
.sort((a, b) => a - b)
.join();
console.log(result); //'45, 66, 80, 88, 90'
}