복습: 은행 계좌
Coddy의 Ruby 개발자를 위한 JavaScript 코스 레슨. 48개 중 37번째.
챌린지
쉬움입력은 {balance,withdrawals}이며, 모든 숫자는 음이 아닌 정수입니다. Account.withdraw(amount)는 amount가 잔액을 초과하면 false를 반환하고, 그렇지 않으면 잔액에서 차감한 뒤 true를 반환합니다. 각 출금에 대한 불리언 값을 나열한 accepted와 balance를 포함하는 {balance,accepted}를 반환하세요.
입력은 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)));
Ruby 개발자를 위한 JavaScript의 모든 레슨
1Ruby에서 JavaScript로
JavaScript 실행하기보간과 템플릿Ruby의 지역 변수에서 let과 const로정수에서 Number로nil, null, 그리고 undefined복습: 영수증4Enumerable에서 Array로 이어지는 파이프라인
while, next, continueEnumerable에서 Array로의 콜백inject와 빈 리덕션sort와 숫자 비교자복습: 순위표 만들기7Ruby 객체에서 JS 클래스까지
initialize에서 constructor로인스턴스 변수와 상태메서드 객체와 this클래스 메서드에서 static으로Reader에서 Getter와 #필드로복습: 은행 계좌직접 연습해 보세요: 온라인 JavaScript 컴파일러