Skip to content

Commit 00a5db6

Browse files
committed
update script SkillfactoryCoding#8 fix less button & fix modalAlert
1 parent a5c809a commit 00a5db6

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

bjs/08_if_else/script.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ let firstRun = true;
4242
function Start()
4343
{
4444
minValue = parseInt(minValueField.value) || 0;
45-
maxValue = parseInt(maxValueField.value) || 100;
45+
maxValue = parseInt(maxValueField.value) === 0 ? 0 : parseInt(maxValueField.value) || 100;
4646

4747
if (minValue > maxValue)
4848
{
@@ -60,13 +60,10 @@ function Start()
6060

6161
orderNumberField.innerText = orderNumber++;
6262
answerField.innerText = `${nextAnswerPhrases[0]} ${IntToText(answerNumber)}?`;
63-
64-
if (firstRun)
65-
{
66-
modalStartAlertText.innerText = `Загадайте любое целое число от ${minValue} до ${maxValue}, а я его угадаю`;
67-
modalStartAlert.modal("show");
68-
firstRun = false;
69-
}
63+
64+
modalStartAlertText.innerText = `Загадайте любое целое число от ${minValue} до ${maxValue}, а я его угадаю`;
65+
modalStartAlert.modal("show");
66+
firstRun = false;
7067
}
7168

7269
// Перевод числовой записи в пропись
@@ -165,19 +162,24 @@ document.querySelector('#btnOver').addEventListener('click', function () {
165162
// Кнопка меньше
166163
document.querySelector('#btnLess').addEventListener('click', function () {
167164
if (gameRun){
168-
if (minValue === maxValue){ // Игрок сжулничал и давал не правильные ответы
169-
const phraseRandom = Math.round( Math.random() * (failAnswerPhrases.length - 1));
170-
const answerPhrase = failAnswerPhrases[phraseRandom];
171-
answerField.innerText = answerPhrase;
172-
gameRun = false;
173-
} else { //Продолжить поиск ниже
165+
if (minValue !== maxValue){ //Продолжить поиск ниже
174166
maxValue = answerNumber - 1;
175-
answerNumber = Math.floor((minValue + maxValue) / 2);
176-
orderNumberField.innerText = orderNumber++;
167+
if (maxValue >= minValue)
168+
{
169+
answerNumber = Math.ceil((minValue + maxValue) / 2);
170+
orderNumberField.innerText = orderNumber++;
177171

178-
const phraseRandom = Math.round( Math.random() * (nextAnswerPhrases.length - 1));
179-
answerField.innerText = `${nextAnswerPhrases[phraseRandom]} ${IntToText(answerNumber)}?`;
180-
}
172+
const phraseRandom = Math.round( Math.random() * (nextAnswerPhrases.length - 1));
173+
answerField.innerText = `${nextAnswerPhrases[phraseRandom]} ${IntToText(answerNumber)}?`;
174+
return;
175+
}
176+
}
177+
// Игрок сжулничал и давал не правильные ответы
178+
const phraseRandom = Math.round( Math.random() * (failAnswerPhrases.length - 1));
179+
const answerPhrase = failAnswerPhrases[phraseRandom];
180+
answerField.innerText = answerPhrase;
181+
gameRun = false;
182+
181183
}
182184
});
183185

0 commit comments

Comments
 (0)