DAY 8 - javascript

연주·2022년 11월 21일
0

TIL

목록 보기
16/37

22.11.21 월

DAY 8 - javascript (기초다지기)

📌 배열매서드 복습

arr 배열의 마지막값
arr[arr.length-1]

push() : 배열의 맨 뒤에 요소 추가
pop() : 배열의 맨 마지막 요소 제거
shift() : 배열의 맨 첫번째 요소 제거
unshift() : 배열의 맨 첫번째 요소 추가

splice(지우고 싶은 자리, 개수 , 지워진 자리에 추가할 요소)
개수 안 정하면 지우고 싶은자리 부터 뒤에 싹 다 제거

includes() ()안에 찾는 요소를 입력하고 있으면, true 없으면 false

indexOf() () 안에 찾는 요소의 index번호를 알려준다.

const arr = ['a','b','c','d','j','e','f','j']
// j 모두 제거
// indexOf, splice 사용

//정답
while (arr.indexOf("j") > -1) {
  arr.splice(arr.indexOf("j"), 1);

// indexOf 값을 찾고 싶을때, -1이 나오기전까지 찾으면 다 찾을 수 있음

// 뭔가 쉽지 라고 생각하고 풀었는데, 풀자마자 어...왜 안되지..
// 처음에 생각한 것은 j의 자리를 다 구해서, 그 자리를 다 빼려고 했는데
// 빼나가면 index가 변한다.
// 그리고 일단 반복문 하면 for부터 냅다 써버릇한다..
// 생각을 하고 써보자ㅜㅜ


📌 배열매서드 연습 문제 풀어보기

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.sort((a, b) => b - a);
 console.log(result);

Q4. make new array without the first two elements

  const array = [1, 2, 3, 4, 5];
  array.splice(0, 2);
  console.log(array);

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) => student.score === 90);
  console.log(result);

// find() 매서드를 사용할 줄 몰랐다.

Q6. make an array of enrolled students

const result = students.filter((student) => student.enrolled);
 console.log(result);

Q7. make an array containing only the students' scores

result should be: [45, 80, 90, 66, 88]

  const result = students.map((student) => student.score);
  console.log(result);

Q8. check if there is a student with the score lower than 50

  //const result = students.includes((student) => student.score < 50);
  const result = students.some((student) => student.score < 50);
  console.log(result);
  

// includes를 사용하려 했지만 값이 안나왔고
// some()을 새로 배우게 됬다

Q9. compute students' average score

  const result = students.reduce((a, b) => a + b.score, 0);
  console.log(result);

// reduce() 매서드 활용이 너무 좋은데, 원래 쓰던대로 쓰려고 했는데 값이 안나왔다
// reduce() 매서드 공부가 더 필요!!!!

Q10. make a string containing all the scores

result should be: '45, 80, 90, 66, 88'

 const result = students.map((student) => student.score).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((a, b) => a - b)
    .join();
  console.log(result);

💬 오늘의 느낌점
스스로 정한 틀에 박힌 순서대로만 하려고 했었는데, 어느정도 필요에 따라 융통성 있게 그날 그날의 학습목표나 학습량이 필요하다고 하셨다.

그래서 계속 부족하다고 생각했던 배열매서드를 정리해보고, 사용할 수 있게 문제를 풀었다.
잘 풀리는 건 풀면 쾌감이 생기는 거 같다. 안 풀리는건 답답하지만..
모르는 부분은 정리해서 어떻게 풀어나가면 좋을 지 생각하고 실천해보자~!

profile
성장중인 개발자🫰

0개의 댓글