A
A
Alexander Shagin2018-03-05 04:18:36
JavaScript
Alexander Shagin, 2018-03-05 04:18:36

Why doesn't && work in a script?

I don't understand why the code doesn't work.
I want to count how many times the user has won in a row.
code
a = prompt('What is the name of the creator of the social network Vkontakte?').toLowerCase();
if (a == "pavel durov" || a == "pavel" || a == 'pavel' || a == 'pavel durov' && count == 3) {
alert you answer correctly!')
count++;
} else if (a == "pavel durov" || a == "pavel" || a == 'pavel' || a == 'pavel durov' && count == 2) {
alert ("Congratulations, you answered correctly twice in a row!")
count++;
} else if (a == "pavel durov" || a == "pavel" || a == 'pavel' ||
a == 'pavel durov' && count == 1) { alert('Congratulations on correct answer.')
count++;
} else {
alert ('The answer is not correct, we recommend reading Biographies more.')
}
My problem is that && does not react, i.e. if the condition that I set is met only one, it still produces exactly what should be issued only with two truths.
the whole code did not fit, but I think the essence of the problem is clear.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrew, 2018-03-05
@qoober

As already suggested, it does not work due to incorrectly placed parentheses, and you should also change the check a little, without so many ifs

a = prompt('Как зовут создателя Соц. сети Вконтакте?').toLowerCase();
if (a == "павел дуров" || a == "павел" || a == 'pavel' || a == 'pavel durov') {
  switch(count) {
    case 1: 
      alert('Поздравляем ответ верный.')
      break;
    case 2: 
      alert ("Поздравляем вы ответили верно два раза сподряд!")
      break;
    case 3: 
      alert ('Потрясающе уже три раза сподряд вы отвечаете верно!')
      break;
  }
  count++;
} else {
  alert ('Ответ не верный, рекомендуем больше читать Биографии.')
}

And the output of congratulations is better to display a separate function

S
Stalker_RED, 2018-03-05
@Stalker_RED

In order for the OR operator to return true, it is enough for any of the conditions to be true. When one of the conditions is met, what is written to the left is not checked.
But you can parenthesize

if ( (условие1 ИЛИ условие2 ИЛИ условие3) И условие4) { . . . }

D
Daniel, 2018-03-05
@daniil14056

Parentheses take everything up to && you check not with the whole expression but with the penultimate one a == 'pavel durov' && count == 3

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question