드림코딩 by 엘리 - 유용한 10가지 배열 함수들 - 퀴즈 정답

bangina·2020년 6월 26일
0

정답은 아니고 혼자 풀어본 코드이니 아 이렇게도 푸는구나~하고 참조해주세요!
정확한 답은 깨끗한 엘리님의 코드로 확인🌞

자바스크립트 9. 유용한 10가지 배열 함수들. Array APIs 총정리 | 프론트엔드 개발자 입문편 ( JavaScript ES6)

<script>
 // Q1. make a string out of an array
 const fruits = ['apple', 'banana', 'orange'];
 const fruitsString = fruits.toString();
 console.log(fruitsString);
 console.log(fruits.join());


 // Q2. make an array out of a string
 const fruits2 = '🍎, 🥝, 🍌, 🍒';
 const fruitsArray = fruits2.split(",");
 console.log(fruitsArray);


 // Q3. make this array look like this: [5, 4, 3, 2, 1]
 const array = [1, 2, 3, 4, 5];
 const arrayReverse = array.reverse();
 console.log(arrayReverse);


 // Q4. make new array without the first two elements
 const array2 = [1, 2, 3, 4, 5];
 const array3 = array2.slice(2, 5);
 console.log(array3);

 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 student90 = students.filter(student => student.score === 90);
 console.log(student90[0].name);


 // Q6. make an array of enrolled students
 let enrolledArray = [];
 const enrolledStudents = students.filter(student => student.enrolled === true).forEach(e => enrolledArray.push(e
     .name));
 console.log(enrolledArray);


 // Q7. make an array containing only the students' scores
 // result should be: [45, 80, 90, 66, 88]
 let scoresArray = [];
 students.forEach(student => scoresArray.push(student.score));
 console.log(scoresArray);
 
 
 // Q8. check if there is a student with the score lower than 50
 let lowScoreStudents = students.filter(student => student.score < 50);
 console.log(lowScoreStudents, 1);


 if (lowScoreStudents == false) {
     console.log("There is no student with the score lower than 50");
 } else {
     console.log("There are students with the score lower than 50");
 }

 // Q9. compute students' average score
 // const scoreSum = scoresArray.forEach(score => score);
 let scoreSum = 0;
 for (let i = 0; i < scoresArray.length; i++) {
     scoreSum += scoresArray[i];
 }
 const averageScore = scoreSum / students.length;
 console.log(averageScore);


 // Q10. make a string containing all the scores
 // result should be: '45, 80, 90, 66, 88'
 const scoresString = scoresArray.join();
 console.log(scoresString);


 // Bonus! do Q10 sorted in ascending order
 // result should be: '45, 66, 80, 88, 90'
 console.log(scoresArray.sort().join());
</script>

profile
UX, Graphic에 관심이 많은 주니어 프론트엔드 개발자. 취미는 요가와 영상편집입니다. 이모지 Lover 💘

0개의 댓글