[자바스크립트] 구구단 문제
개발 •
![[자바스크립트] 구구단 문제](https://cdn-t.marshallku.com/images/images/2020/01/question.png)
자바스크립트 강의 볼만한 거 없나 싶어서 여기저기 기웃거리다 구구단 문제를 구현하는 강의가 있길래 재밌겠다 싶어서 만들어봤습니다.
한 5분 만에 써내려간 코드라 좀 허술합니다…HTML로 넘기기 귀찮아서 자바스크립트의 prompt()와 alert()를 이용했습니다만, 아주 못난 디자인이라 어지간하면 html로 넘기시는 걸 추천합니다.
심지어 prompt()를 쓰다 보니 모바일에선 훨씬 문제 풀기 불리하더라고요.
const gugudan = () => {
const user = {
correct: 0,
incorrect: 0,
time: []
};
let max = 8;
const problem = () => {
let first, second;
const setnum = () => {
const min = 0.5 * user.correct;
max = Math.floor(0.1 * user.correct + max);
first = Math.floor(min + Math.random() * max) + 1;
second = Math.floor(min + Math.random() * max) + 1;
};
const question = () => {
const now = new Date();
const prm = prompt(`${first} * ${second} = ?`, "");
if (prm === null) {
const arr = user.time;
const max = arr.length;
let sum = 0;
for(let i = 0; i < max; i++) {
sum += arr[i] / 1000
}
alert(`정답 : ${user.correct}\n오답 : ${user.incorrect}\n평균 : ${(sum/max).toFixed(3)}초`)
}
else {
+prm === (first * second)
? (
user.correct++,
user.time.push(new Date() - now),
setnum(),
question()
)
: (
alert("Incorrect!"),
user.incorrect++,
user.time.push(new Date() - now),
question()
)
}
};
setnum(),
question();
}
problem();
};
gugudan()
제목은 구구단인데, 한 자리 곱셈만 하고 있으면 재미없으니, 문제를 맞추면 난도가 증가하도록 해뒀습니다.
여담인데, 두 자리 곱셈을 암산으로 안 한 지가 오래라, 15 * 17 같은 단순한 문제도 당황스럽더라고요.

문제를 풀지 않고 취소를 누르면 정답 수, 오답 수, 문제를 푸는데 평균적으로 걸린 시간이 출력됩니다.
ⓒ 2020. Marshall K All rights reserved