//Q1. make a string out of an array
{
const fruits = ['apple', 'banana', 'orange'];
//console.log(fruits.toString());
console.log(fruits.join(' and '));
}
//Q2. make an array out of a string
{
const fruits = '๐,๐,๐,๐ญ';
const result = fruits.split(',', 2); // ',' ๊ธฐ์ค์ผ๋ก 2๊ฐ๋ง ์ถ๋ ฅํ๋ผ. ์๋ก์ด ๋ฐฐ์ด return
console.log(result);
}
//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); // ๊ธฐ์กด ๋ฐฐ์ด๋ ๋ค์งํ
}
//Q4. make new array without the first two elements
{
const array = [1, 2, 3, 4, 5];
// const newArr = array.splice(0, 2); // ๊ธฐ์กด ๋ฐฐ์ด์ด ์๋ฆฐ๋ค. 0 ๋ถํฐ 2๊ฐ ์๋ผ์ค
// console.log(array); // [3, 4, 5]
// console.log(newArr); // [1, 2]
const newArr = array.slice(2, 5); // ์๋ก์ด ๋ฐฐ์ด์ return. 2๋ฒ index ๋ถํฐ 5 - 1๋ฒ index๊น์ง
console.log(newArr); // [3, 4, 5]
}
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
{
// for(const sub of students)
// {
// if(sub.score === 90)
// console.log(sub);
// }
// const result = students.find(function (student, index){
// //console.log(student, index);
// return student.score === 90;
// });
const result = students.find((student) => student.score === 90);
console.log(result);
}
console.clear();
//Q6. make an array of enrolled students
{
const result = students.filter((student) => student.enrolled === true);
console.log(result);
}
console.clear();
//Q7. make an array containing only the student's scores
//result should be: [45, 80, 90, 66, 88]
//๋ฐ์ ๊ฐ์ ์ด๋ป๊ฒ ์ฒ๋ฆฌํด์ ๋๊ฒจ ์ค์ง ์ ํด์ค๋ค.
{
const result = students.map((student) => student.score * 2);
console.log(result); // [90, 160, 180, 132, 176] return
}
console.clear();
//Q8. check if there is a student with the score lower than 50
{
const result = students.some((student) => student.score < 50); // some ์ boolean
console.log(result);
}
console.clear();
//Q9. compute students' average score
{
const result = students.reduce((prev, curr) => prev + curr.score, 0);
console.log(result);
}
//Q10. make a string containing all the scores
// result should be: '45, 66, 80, 90
{
const resultl = students.map((student) => student.score)
.filter((score) => score >= 50) // 50์ ์ด์์ธ ์ ๋ค๋ง filtering
.join();
console.log(resultl);
}
console.clear();
//sort
{
const result = students
.map((student) => student.score)
.sort((a, b) => a - b)
.join();
console.log(result);
}
splice โ ์๋ฆฐ elem return, ๊ธฐ์กด ๋ฐฐ์ด๋ ์๋ฆผ
slice โ start ~ end ์ด์ ๊น์ง ํด์ ์๋ก์ด ๋ฐฐ์ด return, ๊ธฐ์กด ๋ฐฐ์ด์ ๊ทธ๋๋ก
๋ฌธ์์ด split โ arg ๊ธฐ์ค์ผ๋ก ๋๋, ์๋ก์ด ๋ฐฐ์ด return, ๊ธฐ์กด ๋ฐฐ์ด์ ๊ทธ๋๋ก