드림코딩 자바스크립트 기초강의 정리9

Dongwoo Kim·2021년 7월 16일

유용한 10가지 배열 함수들

//퀴즈
// Q1. make a string out of an array
{
  const fruits = ['apple', 'banana', 'orange'];
}
let str='';
for (let i=0; i<fruits.length; i++) {
	str.push(fruits[i]);
}
//정답
fruits.join();

// Q2. make an array out of a string
{
  const fruits = '🍎, 🥝, 🍌, 🍒';
}
let arr = [];
for (let i = 0; i < fruits.length; i++) {
	arr.push(fruits[i]);
}

//정답
fruits.split(',');

// Q3. make this array look like this: [5, 4, 3, 2, 1]
{
  const array = [1, 2, 3, 4, 5];
}
//1
array.reverse();
//2
let arr = []
for (let i = array.length-1; i > 0; i--) {
	arr.push(array[i]);
}
array.reverse();

//풀이 : reverse는 원래 배열도 바꾼다.


// Q4. make new array without the first two elements
{
  const array = [1, 2, 3, 4, 5];
}

array.shift()
array.shift()

//정답X
const result = array.splice(0, 2);
console.log(result); // [1,2]
console.log(array); // [3, 4, 5]

//정답O
const result = array.slice(2, 5);
console.log(result); // [3, 4, 5];
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),
];

// Q5. find a student with the score 90
{
  for (let student of students) {
   if (Student[score] === 90) return Student;
  }
  
  //정답
  const result = students.find((student) => return student.score === 90);
}

// Q6. make an array of enrolled students
{
  let arr = []
   for (let student of students) {
   if (Student[enrolled] === true) arr.push(Student);
  }
  
  //정답
  const result = students.filter((student) => return student.enrolled);
}

// Q7. make an array containing only the students' scores
// result should be: [45, 80, 90, 66, 88]
{
   let arr = []
   for (let student of students) {
   	arr.push(Student[score]);
  }
  
  //정답
  students.map((student) => student.score)
}

// Q8. check if there is a student with the score lower than 50
{
   for (let student of students) {
   	if (Student[score] < 50) return true;
    else return false;
  }
  
  //정답: 조건이 맞는 몇 개의 결과 
  const result = students.some((student) => student.score < 50);
  
  //every 조건에 맞는 모든 결과
}

// Q9. compute students' average score
{
   let arr = []
   for (let student of students) {
   	arr.push(Student[score]);
  }
 const sum = arr.reduce((a, b) => a + b, 0);
 const average = sum / arr.length;
  
  //정답
  const result = students.reduce((prev, curr) => prev + curr.score, 0);
   console.log(result / students.length);
}

// Q10. make a string containing all the scores
// result should be: '45, 80, 90, 66, 88'
{
   let str = '';
   for (let student of students) {
   	arr.push(Student[score]);
  }
  
    //정답
  const result = students
  	.map((student) => student.score)
  	.filter((score) => score >= 50)
  	.join();
  
  console.log(result);
}

// Bonus! do Q10 sorted in ascending order
// result should be: '45, 66, 80, 88, 90'
{
   let arr = []
   for (let student of students) {
   	arr.push(Student[score]);
  }
  arr.sort((a, b) => a - b);
	
  //정답
	const result = students.map((student) => student.score)
    .sort((a, b) => a - b)
    .join()
    console.log(result);
}
profile
水滴石穿

0개의 댓글