본문 바로가기
TIL

20230902 TIL : 중꺾마,, !

by 김빵그 2023. 9. 2.

Today 요약

[] 프로그래머스 1문제

[] dart 강의 3개


배운 점

1) array forEach 

 

Array.prototype.forEach() - JavaScript | MDN

forEach() 메서드는 주어진 함수를 배열 요소 각각에 대해 실행합니다.

developer.mozilla.org

  arr.forEach(callback(currentvalue[, index[, array]])[, thisArg])
  • callback : 각 요소에 대해 실행할 함수, 다음 세가지 매개변수를 받는다
  • currentValue : 현재 처리할 요소
  • index : 처리할 요소의 인덱스
  • array : forEach를 호출한 배열
  • thisArg : callback을 실행할 때 this로 사용할 값

1-1 설명

  • forEach는 주어진 콜백을 배열에 있는 각 요소에 대해 오름차순으로 한번씩 실행하는 것
  • 삭제했거나 초기화하지 않은 인덱스 속성에 대해서는 실행하지 않는다

1-2 초기화하지 않은 값의 반복 생략 

const arraySparse = [1, 3, , 7];
let numCallbackRuns = 0;

arraySparse.forEach(function (element) {
  console.log(element);
  numCallbackRuns++;
});

console.log("numCallbackRuns: ", numCallbackRuns);

// 1
// 3
// 7
// numCallbackRuns: 3
// comment: as you can see the missing value between 3 and 7 didn't invoke callback function.

1-3 for 반복문을 forEach로 변경

const items = ["item1", "item2", "item3"];
const copy = [];

// 이전
for (let i = 0; i < items.length; i++) {
  copy.push(items[i]);
}

// 이후
items.forEach(function (item) {
  copy.push(item);
});

1-4 배열 출력

function logArrayElements(element, index, array) {
  console.log("a[" + index + "] = " + element);
}

// 인덱스 2는 배열의 그 위치에 항목이 없기에
// 건너뜀을 주의하세요.
[2, 5, , 9].forEach(logArrayElements);
// 기록:
// a[0] = 2
// a[1] = 5
// a[3] = 9

2) Var? 

라나트에

  • variable = 변수
  • 변수를 생성하는 키워드

3) final / const

  • 비슷한 점 : 한번 고정된 변수는 고칠 수 없음
  • final ? 계산, 데이터 서버에서 가져온다던가, 데이터 베이스 값 
  • 앱 실행 전 변수와 값이 뭔지 알면 const / 앱 실행 후 runtime에 값이 뭔지 알게 되면 final

4) Effective Dart style

 

Effective Dart: Style

Formatting and naming rules for consistent, readable code.

dart.dev

  • UpperCamlcase : 첫 번째 단어를 포함해 각 단어의 첫 글자를 대문자
  • lowerCamelCase : 각 단어 첫글자를 대문자로 표기 단 첫 글자는 약어라도 항상 소문자
  • lowercase_with_underscores : 약어라돗 ㅗ문자만사용하고 단어는 _ 구분