200729_TIL

oh_ji_0·2020년 7월 29일
1

TIL

목록 보기
3/61

Today I Leared

  • 모르는 것을 검색하는 방법
  • 반복문
  • 배열

[검색 방법]

  • 유용한 검색 결과를 뽑아내는 법
  • 질문의 주요 키워드 선별
  • mdn 검색
  • stackoverflow 답변 활용
  • 콘솔창 에러메시지 활용

[반복문]

  • 같거나 비슷한 코드를 여러번 실행시켜야 할 경우, 반복 조건을 코드로 작성
    [초깃값, 조건식, 증감문]
  • 슈도코드(의사코드)를 활용.
    일반적 언어로 코드를 흉내내어 알고리즘을 쓰는 것
  • for 구문
for(let i 초기화 ;조건식; 증감문){
   //반복구문
}

for 구문 초기화에서 let 변수 선언을 해줘야한다.
for 문 안에서 let을 붙이지 않으면 자동으로 var가 붙는다.

  • while 구문
//초기화
while(조건식){
  //증감문
  //반복구문
}
  • do while 구문
초기화
do{
  //증감문
  //반복구문
}while(조건식)
  
//조건식에 맞지 않아도, 초기 한번은 반드시 실행된다.
  • switch문
switch(조건문){
  case1:
    //실행구문
    [break;]
  case2:
    //실행구문
    [break;]
  caseN:
    //실행구문
	[break;]
  default:
    //실행구문
    [break;]
}
  • break / continue

    1) break 문법
    break;
    break 레이블; (특정 레이블 문을 빠져나옴)

    2) continue
    while, do-while, for, 레이블 문을 다시 시작하기 위해 사용
    continue;
    continue 레이블;

    continue와 break문은 반복문에서만 사용한다.

//break
  for (i = 0; i < a.length; i++) {
    if (a[i] == theValue) {
      break;
    }
  }

//break 레이블
  var x = 0;
  var z = 0
  labelCancelLoops: while (true) {
    console.log("Outer loops: " + x);
    x += 1;
    z = 1;
    while (true) {
      console.log("Inner loops: " + z);
      z += 1;
      if (z === 10 && x === 10) {
        break labelCancelLoops;
      } else if (z === 10) {
        break;
      }
    }
  }

//continue
  i = 0;
  n = 0;
  while (i < 5) {
    i++;
    if (i == 3) {
      continue;
    }
    n += i;
  }

//continue 레이블
  checkiandj:
  while (i < 4) {
    console.log(i);
    i += 1;
    checkj:
      while (j > 4) {
        console.log(j);
        j -= 1;
        if ((j % 2) == 0) {
          continue checkj;
        }
        console.log(j + " is odd.");
      }
      console.log("i = " + i);
      console.log("j = " + j);
  }
  • for ...in 문
//초기화
  for( variable in object ) {
      //실행구문
  }

  function dump_props(obj) {
    var result = "";
    for (var i in obj) {
      result += i;
    }
    return result;
  }
  //result : key값 출력, 속성이름 반복
  • for...of문
  for (variable of object) {
    //실행구문
  }

  //속성값 반복
  • Number.isInteger()
    정수인지 여부를 반환.

[배열]

const array1 = [1, 2, 3];
console.log(array1.unshift(4, 5));
// expected output: 5
console.log(array1);
// expected output: Array [4, 5, 1, 2, 3]
  • arr.push()
const animals = ['pigs', 'goats', 'sheep'];
const count = animals.push('cows');
console.log(count);
// expected output: 4
console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows"]
  • arr.reduce()
const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;
// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10
// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15
  • arr[i] 값에 대입 되지 않음.
  • arr1.concat(arr2)
  • forEach()
    arr.forEach(callback(currentvalue[, index[, array]])[, thisArg])

[문자열]

  • arr.indexOf()
    문자열에서 원하는 글자의 index 반환
  • typeof 연산자
console.log(typeof 'blubber');
// expected output: "string"

[디버깅]

  • js에선 return 값을 따로 지정하지 않을 경우 undefined를 반환
  • 지역변수 let과 스코프
  • debugger
    global, local, block 구역에 따른 변수들의 값 변화 확인

[추가공부]

  • 배운 개념 복습 블로깅 및 학습 노트 정리
  • 코어 자바스크립트 책 독서

[Comment]

@@ 문자열 복습 및 반복문에 대해서 주로 배우고, 배열에 대해서 조금 예습하는 시간을 가졌다. 첫번째 페어분과의 시간을 오늘로서 종료하고 내일은 다른 페어분을 만나게 된다. 리뷰하는 경험도 처음 해보고, 마지막에 헤어지며 페어분과 덕담도 주고 받았다. 앞으로도 지치지 말고 열심히 페어 활동에 참여해야겠다 생각했다. 첫 페어라 모두가 그렇겠지만, 나 역시도 미숙하고 얼떨떨했는데 참여를 열심히 해주신 페어분께 감사드린다.🙇🙇

profile
기본에 충실하고 싶습니다. #Front-end-developer

0개의 댓글