// Q1. make a string out of an array
{
const fruits = ['apple', 'banana', 'orange'];
console.log(fruits.join(','));
}
// Q2. make an array out of a string
{
const fruits = '🍎, 🥝, 🍌, 🍒';
const result = fruits.split(','); //구분자 전달 안하면 length 1개
console.log(result);
}
// Q3. make this array look like this: [5, 4, 3, 2, 1]
{
const array = [1, 2, 3, 4, 5];
const reverseArray = array.reverse()
console.log(reverseArray);
console.log(array);
}
// Q4. make new array without the first two elements
// 새 배열이므로 splice(원본 훼손) 하지 말고 slice 해야 한다.
{
const array = [1, 2, 3, 4, 5];
const result = array.slice(2, 5);
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
// 찾은 애를 return하는 find() 쓰는게 좋음
{
const result = students.find((student) => student.score === 90);
console.log(result);
}
// Q6. make an array of enrolled students
{
const result = 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]
{
const result = students.map((student) => student.score);
console.log(result);
}
// Q8. check if there is a student with the score lower than 50
{
console.clear();
const result = students.some((student) => student.score < 50);
console.log(result);
const result2 = !students.every((student) => student.score >= 50);
console.log(result2);
}
// Q9. compute students' average score
{
const result = students.reduce((acc, curr) => acc + curr.score, 0);
console.log(result / students.length);
}
// Q10. make a string containing all the scores
// result should be: '45, 80, 90, 66, 88'
{
const result = students.map((student) => student.score)
.join(', ')
// const avgArr = students.map((student) => student.score)
// .filter((score) => score > 50);
console.log(result);
}
// Bonus! do Q10 sorted in ascending order
// result should be: '45, 66, 80, 88, 90'
{
const result = students.map((student) => student.score)
.sort((a, b) => a - b)
// .sort((a, b) => b - a) 내림차순
.join(', ');
console.log(result);
}