티스토리 뷰

Skill/html js css

[javascript] 배열 함수들

진열사랑 2020. 10. 2. 12:18

출처 : www.youtube.com/watch?v=3CUjtKJ7PJg [드림코딩 by 엘리]

// 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(',',3); // second parameter는 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 result1 = array.splice(0,2);
  // console.log(result1);
  // console.log(array); // 배열 자체가 변화된다.
  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
{
  const result1 = students.find(function(student,index,arr){
    return student.score == 90;
  });
  const result = students.find( (student) => student.score == 90 );
  console.log(result); // student.score가 90인 첫 student를 반환
}

// Q6. make an array of enrolled students
{
  const result = students.filter(function(student) {
    return student.enrolled;
  });
  const result2 = students.filter((student) => student.enrolled);
  console.log(result); // student.enrolled 가 true인 student의 배열 반환
  console.log(result2);
  
}

// Q7. make an array containing only the students' scores
// result should be: [45, 80, 90, 66, 88]
{
  const result = students.map(function(student){
    return student.score;
  });
  const result2 = students.map( (student) => student.score );
  console.log(result); // student.score의 배열 반환
  console.log(result2);
}

// Q8. check if there is a student with the score lower than 50
{
  const result = students.some( (student) => student.score < 50);
  const result2 = students.every( (student) => student.score >= 40);
  console.log(result); // true
  console.log(result2);
}

// Q9. compute students' average score
{
  const result = students.reduce(function(prev, curr) {
    console.log(prev,curr);
    return prev + curr.score; // 반환하는 값이 다음 호출 때의 prev 값으로 들어온다.
  }, 0); // 0 첫 호출 때의 prev값
  const result2 = students.reduce((prev,curr)=> prev+curr.score, 0);
  console.log(result);
  console.log(result2);
}

// Q10. make a string containing all the scores
// result should be: '45, 80, 90, 66, 88'
{
  const result = students
    .map( (student)=>student.score) // student.score로 이루어진 배열 반환
    .filter( (score)=> score >= 50) // score배열 중 score 50 이상인 배열 반환
    .join();
  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(function(a,b) {
      return a-b; // 반환값이 0보다 작은 경우 a를 b보다 낮은 색인으로 정렬합니다
    })
    .join();
  const result2 = students.map(student=>student.score)
    .sort((a,b)=>(a-b))
    .join();
  console.log(result);
  console.log(result2);
}

'Skill > html js css' 카테고리의 다른 글

[javascript] bind()사용법  (0) 2020.10.05
[javascript] promise  (0) 2020.10.02
JQuery 첫 실행 문장들 비교  (0) 2020.09.28
[javascript] prototype  (0) 2020.09.24
[javascript] this가 가리키는 것은?  (0) 2020.09.24
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함