Menu
CoddyTech

복습: 은행 계좌

Coddy의 Dart 개발자를 위한 JavaScript 코스 레슨. 48개 중 37번째.

challenge icon

챌린지

쉬움

입력은 {balance,withdrawals}이며, 모든 숫자는 음이 아닌 정수입니다. Account.withdraw(amount)는 amount가 balance를 초과하면 false를 반환하고, 그렇지 않으면 balance에서 amount를 빼고 true를 반환합니다. 각 출금에 대한 불리언 값을 나열한 accepted와 balance를 반환하세요.

입력은 JSON 값 하나입니다. 잠금이 해제된 파일을 수정하세요. exercise.js부터 시작하면 됩니다. 화면에 표시된 잠금 파일 driver.js는 solve(input)을 호출하고, 반환값을 압축된 JSON으로 출력한 뒤 줄바꿈을 하나 출력합니다. 진단용 출력을 추가하지 마세요. 테스트는 전체 출력을 비교합니다. 모든 숫자 입력값과 계산된 정수 합계는 안전한 정수 범위 안에 있습니다.

예시 입력:

{"balance":10,"withdrawals":[4,7,6]}

예상 출력:

{"balance":0,"accepted":[true,false,true]}

잠금 파일은 solve를 호출하기 전에 새 인스턴스로 내보낸 클래스 API도 직접 확인합니다. 이름이 지정된 클래스와 문서화된 필드 및 메서드를 구현하세요. 최종 출력만 재현하는 것으로는 충분하지 않습니다. driver.js에서 모든 고정 검사를 확인할 수 있습니다.

직접 해보기

import assert from "node:assert/strict";
// 고정된 API 검증: 클래스 모듈은 과제 명세의 일부입니다.
import { Account } from "./Account.js";
const first=new Account(10), second=new Account(4);
assert.equal(first.withdraw(4),true); assert.equal(first.balance,6);
assert.equal(first.withdraw(7),false); assert.equal(first.balance,6);
assert.equal(first.withdraw(6),true); assert.equal(first.balance,0);
assert.equal(first.withdraw(0),true); assert.equal(second.balance,4);
import { readFileSync } from "node:fs";
import { solve } from "./exercise.js";
// stdin으로 JSON 값 하나가 들어옵니다. 데이터를 반환하고 solve 내부에서는 출력하지 마세요.
const input = JSON.parse(readFileSync(0,"utf8"));
console.log(JSON.stringify(solve(input)));

Dart 개발자를 위한 JavaScript의 모든 레슨

직접 연습해 보세요: 온라인 JavaScript 컴파일러