// Q. make a string out of an array
const fruits = ['mango', 'melon', 'peach'];
const result = fruits.join();
const result2 = fruits.join(" ");
console.log(result); // mango,melon,peach
console.log(result2); // mango melon peach
console.log(typeof result); // "string"
// Q. make an array out of a string
const fruits = 'cherry,melon,peach';
const result = fruits.split(',');
console.log(result); // ["cherry", "melon", "peach"]
// Q. result should be: [5, 4, 3, 2, 1]
const arr = [1, 2, 3, 4, 5]
const result = arr.reverse();
console.log(result); // [5, 4, 3, 2, 1]
console.log(arr); // [5, 4, 3, 2, 1]
// Q. Returns a section of an array.
const array = [1, 2, 3, 4, 5];
const result = array.slice(0, 2);
console.log(result); // [1, 2]
console.log(array); // [1, 2, 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), ]
// Find a student with the score 90
const result = students.find((student) =>
student.score === 90);
console.log(result); // Student {name: "C", age: 30, enrolled: true, score: 90}
const result = students.filter((student) => student.enrolled);
console.log(result); // [Student, Student, Student]
// make an array containing only the students' score
const result = students.map((student) => student.score);
console.log(result); // [45, 80, 90, 66, 88]
// check if there is a student with the score lower than 50
const result = students.some((student) => student.score < 50)
console.log(result); // true
// use every()
const result2 = !students.every((student) => student.score >= 50)
console.log(result2); // true
// compute students' average score
const result = students.reduce((prev, curr) => prev + curr.score, 0)
console.log(result / students.length); // 73,8
//make a string containing all the scores bigger than 50
// result should be: '80, 90, 66, 88'
const result = students
.map((student) => student.score)
.filter((score) => score >= 50)
.join();
console.log(result) // 80,90,66,88
// sorted in ascending order
// result should be: '45, 66, 80, 88 90'
const result = students
.map(student => student.score)
.sort((a, b) => a - b)
.join();
console.log(result) // 45,66,80,88,90
์ถ์ฒ : MDN
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
๋๋ฆผ์ฝ๋ฉ by ์๋ฆฌ
https://www.youtube.com/watch?v=3CUjtKJ7PJg&list=LL&index=1&t=12s
w3schools
https://www.w3schools.com/js/js_array_methods.asp