오늘은 과제를 모두 마무리한 날로, 다음 주 월요일부터 "풋살온라인" 프로젝트를 진행할 예정이다. 오늘 베이직 수업에서 다룬 과제 내용은 아래와 같다.
과제 내용
게임 캐릭터 클래스 구현:
GameCharacter 클래스를 작성하여 캐릭터의 이름, 레벨, 체력, 힘을 속성으로 가졌다.
attack(), heal(), levelUp() 메서드를 구현하여 각각 공격, 체력 회복, 레벨업 기능을 수행하게 했다.
class GameCharacter {
constructor(name, level, stamina, str) {
this.name = name;
this.level = level;
this.stamina = stamina;
this.str = str;
}
attack() {
console.log(`${this.str}의 힘으로 공격합니다!`);
}
heal() {
console.log(`체력을 ${(this.stamina += 20)} 회복했습니다!`);
}
levelUp() {
const level = Math.floor(Math.random() * 20);
this.stamina += level;
this.str += level;
console.log(
`축하합니다!레벨업 하셨습니다!
체력이 ${this.stamina} 에서 ${level} 올랐습니다!
힘이 ${this.str} 에서 ${level} 올랐습니다!`
);
}
}
세 개의 캐릭터 인스턴스를 생성하고 각 캐릭터의 메서드를 호출하여 상태를 출력했다.
const ohs1 = new GameCharacter("토벤머리전사", 1, 100, 30);
const ohs2 = new GameCharacter("zㅣ존검사", 1, 100, 15);
const ohs3 = new GameCharacter("닉넴뭘로하지", 1, 100, 60);
console.log("ohs1의 상태");
ohs1.attack();
ohs1.heal();
ohs1.levelUp();
console.log("ohs2의 상태");
ohs2.attack();
ohs2.heal();
ohs2.levelUp();
console.log("ohs3의 상태");
ohs3.attack();
ohs3.heal();
ohs3.levelUp();
Promise 사용:
보물상자를 여는 작업을 Promise로 구현하였다. 10%의 확률로 성공하고, 90%의 확률로 실패하도록 설정했다.
const openBox = () =>
new Promise((resolve, reject) => {
setTimeout(() => {
const success = Math.random() > 0.1;
if (success) {
resolve("성공");
} else {
reject("실패");
}
}, 3000); // 3초 후에 결과 공개
});
openBox()
.then(() => {
console.log("축하합니다! 황금 보물을 발견했습니다!");
})
.catch(() => {
console.log("보물을 찾는 데 실패했습니다. 다시 시도하세요.");
});
추가 도전 과제:
findTreasure 함수를 만들어 보물 상자를 여는 작업을 시뮬레이션하고, 성공과 실패 메시지를 출력하는 방식으로 구현할 수 있다.
then, catch를 try/catch, async/await로 변경하는 것도 도전 과제로 설정하였다.
오늘 하루 과제를 통해 JavaScript의 클래스, 메서드, Promise 사용법을 복습할 수 있었다. 다음 주 프로젝트 진행에 큰 도움이 될 것으로 기대된다.
'부트캠프' 카테고리의 다른 글
| OSI 7계층 중 물리계열 (0) | 2024.12.02 |
|---|---|
| 5주차 WIL (1) | 2024.11.29 |
| 아이템 시뮬레이션 트러블슈팅 (1) | 2024.11.29 |
| 22일차 TIL (0) | 2024.11.28 |
| 21일차 TIL (0) | 2024.11.27 |