본문 바로가기

Programming Language/Javascript

[강의노트][드림코딩] 배열 Array Quiz 10가지

드림코딩 자바스크립트 배열 강의 노트

 

Q1. make a string out of an array
{
    const fruits = ['apple', 'banana', 'orange'];
    const result = fruits.join();
    console.log(result);
}

Q2. make an array out of a string
{
    const fruits = '🍎, 🥝, 🍌, 🍒';
    const result = fruits.split(',');
    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 result = array.slice(2, 5); // 2부터 5전까지
    console.log(result);
    // splice는 배열 자체를 변경, but slice는 배열은 그대로
}

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
{
    const result = students.find((student)=>{
        return  student.score === 90;
    });
    console.log(result);
}

Q6. make an array of enrolled students
{
   
}

Q7. make an array containing only the students' scores
result should be: [45, 80, 90, 66, 88]
{
    const result = students.map(e => e.score);
    console.log(result);
}

Q8. check if there is a student with the score lower than 50
{
    const result = students.some(e => e.score < 50);
    console.log(result); // 일부 요소 조건 성립 ||

    const result2 = !students.every(e => e.score >= 50);
    console.log(result2); // 모든 요소 조건 성립 &&
   
}

Q9. compute students' average score
{

}

Q10. make a string containing all the scores
result should be: '45, 80, 90, 66, 88'
{
    const result = students
    .map( s => s.score)
    .filter(score => score >= 50)
    .join();

    console.log(result);


}

Bonus! do Q10 sorted in ascending order
result should be: '45, 66, 80, 88, 90'
{
}