Commit 21577f1e authored by Vladislav Rybalko's avatar Vladislav Rybalko

done task 4

parents
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="js/main.js"></script>
</body>
</html>
\ No newline at end of file
const userErrors = new Array(9).fill(0)
let correctAnswer = 0
let wrongAnswer = 0
let questionCount = 0
const quizMode = prompt('Choose difficult mode: \neasy \nnormal \nhard \nall')
if(quizMode !== null) {
quizMode.toLowerCase().trim()
}
switch (quizMode) {
case 'easy' :
alert('easy')
while(true) {
const firstRandomNum = Math.floor(Math.random() * 2 + 1)
const secondRandomNum = Math.floor(Math.random() * 2 + 1)
const userAnswer = prompt(`What is ${firstRandomNum} * ${secondRandomNum}`)
if(userAnswer === null) break;
const userNum = parseInt(userAnswer)
if(!isNaN(userNum)) {
questionCount ++
if(userNum === firstRandomNum * secondRandomNum) {
correctAnswer ++
} else {
wrongAnswer ++
userErrors[firstRandomNum - 1] ++
if(firstRandomNum !== secondRandomNum) {
userErrors[secondRandomNum - 1] ++
}
};
} else {
alert('You enter invalid value!')
}
}
let errorMessage = ''
for (let i = 0; i < userErrors.length; i++) {
errorMessage += `Multiplay by ${i + 1} = ${userErrors[i]}\n`
}
const messageResult = `Total question: ${questionCount} \nCorrect answer: ${correctAnswer} \nWrong answer ${wrongAnswer} \n${errorMessage}`
alert(messageResult)
break;
case 'normal' : {
alert('normal')
while(true) {
const firstRandomNum = Math.floor(Math.random() * 3 + 3)
const secondRandomNum = Math.floor(Math.random() * 3 + 3)
const userAnswer = prompt(`What is ${firstRandomNum} * ${secondRandomNum}`)
if(userAnswer === null) break;
const userNum = parseInt(userAnswer)
if(!isNaN(userNum)) {
questionCount ++
if(userNum === firstRandomNum * secondRandomNum) {
correctAnswer ++
} else {
wrongAnswer ++
userErrors[firstRandomNum - 1] ++
if(firstRandomNum !== secondRandomNum) {
userErrors[secondRandomNum - 1] ++
}
};
} else {
alert('You enter invalid value!')
}
}
let errorMessage = ''
for (let i = 0; i < userErrors.length; i++) {
errorMessage += `Multiplay by ${i + 1} = ${userErrors[i]}\n`
}
const messageResult = `Total question: ${questionCount} \nCorrect answer: ${correctAnswer} \nWrong answer ${wrongAnswer} \n${errorMessage}`
alert(messageResult)
}
break;
case 'hard' : {
alert('hard')
while(true) {
let firstRandomNum = 1
while (
firstRandomNum === 1 ||
firstRandomNum === 2 ||
firstRandomNum === 5 ) {
firstRandomNum = Math.floor(Math.random() * 9 + 1)
}
let secondRandomNum = 1
while (
secondRandomNum === 1 ||
secondRandomNum === 2 ||
secondRandomNum === 5 ) {
secondRandomNum = Math.floor(Math.random() * 9 + 1)
}
const userAnswer = prompt(`What is ${firstRandomNum} * ${secondRandomNum}`)
if(userAnswer === null) break;
const userNum = parseInt(userAnswer)
if(!isNaN(userNum)) {
questionCount ++
if(userNum === firstRandomNum * secondRandomNum) {
correctAnswer ++
} else {
wrongAnswer ++
userErrors[firstRandomNum - 1] ++
if(firstRandomNum !== secondRandomNum) {
userErrors[secondRandomNum - 1] ++
}
};
} else {
alert('You enter invalid value!')
}
}
let errorMessage = ''
for (let i = 0; i < userErrors.length; i++) {
errorMessage += `Multiplay by ${i + 1} = ${userErrors[i]}\n`
}
const messageResult = `Total question: ${questionCount} \nCorrect answer: ${correctAnswer} \nWrong answer ${wrongAnswer} \n${errorMessage}`
alert(messageResult)
}
break;
case 'all': {
alert('all')
while(true) {
const firstRandomNum = Math.floor(Math.random() * 9 + 1)
const secondRandomNum = Math.floor(Math.random() * 9 + 1)
const userAnswer = prompt(`What is ${firstRandomNum} * ${secondRandomNum}`)
if(userAnswer === null) break;
const userNum = parseInt(userAnswer)
if(!isNaN(userNum)) {
questionCount ++
if(userNum === firstRandomNum * secondRandomNum) {
correctAnswer ++
} else {
wrongAnswer ++
userErrors[firstRandomNum - 1] ++
if(firstRandomNum !== secondRandomNum) {
userErrors[secondRandomNum - 1] ++
}
};
} else {
alert('You enter invalid value!')
}
}
let errorMessage = ''
const countErr = []
for (let i = 0; i < userErrors.length; i++) {
countErr.push({ [i + 1]: userErrors[i]})
}
const sortedError = countErr.sort((a, b) => a[Object.keys(a)[0]] - b[Object.keys(b)[0]]).slice(-3)
for (let i = 0; i < sortedError.length; i++) {
const key = Object.keys(sortedError[i])[0]
const value = sortedError[i][key]
errorMessage += `Multiplay by ${key} = ${value}\n`
}
const messageResult = `Total question: ${questionCount} \nCorrect answer: ${correctAnswer} \nWrong answer ${wrongAnswer} \n${errorMessage}`
alert(messageResult)
}
break;
case null :
alert('End game')
break;
default:
alert('You enter invalid value, setting default mode, all')
break;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment