본 튜토리얼은 전 세계 사람들이 이용할 수 있는 오픈 소스 프로젝트입니다. 프로젝트 페이지에 방문하셔서 번역을 도와주세요.
돌아가기

계산기 만들기

중요도: 5

calculator라는 객체를 만들고 세 메서드를 구현해 봅시다.

  • read()에선 프롬프트 창을 띄우고 더할 값 두 개를 입력받습니다. 입력받은 값은 객체의 프로퍼티에 저장합니다.
  • sum()은 저장된 두 값의 합을 반환합니다.
  • mul()은 저장된 두 값의 곱을 반환합니다.
let calculator = {
  // ... 여기에 답안 작성 ...
};

calculator.read();
alert( calculator.sum() );
alert( calculator.mul() );

데모 실행하기

테스트 코드가 담긴 샌드박스를 열어 정답을 작성해보세요.

let calculator = {
  sum() {
    return this.a + this.b;
  },

  mul() {
    return this.a * this.b;
  },

  read() {
    this.a = +prompt('첫 번째 값:', 0);
    this.b = +prompt('두 번째 값:', 0);
  }
};

calculator.read();
alert( calculator.sum() );
alert( calculator.mul() );

테스트 코드가 담긴 샌드박스를 열어 정답을 확인해보세요.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.