JavaScript Why is this while loop not working with an AND logical operator? the while loop,the while loop in python,the while

I'm trying to run a super simple program with javascript but I just can't get my head around this issue. Ok here my code:

var answer = prompt("Are we there yet?");

while(answer != "yes" || answer != "yeah") {
    var answer = prompt("Are we there yet?");
}

alert("Yes! We made it!");

The problem is that when I run this on my browser and type either "yes" or "yeah" the prompt method keeps appearing unless I change the logical operator from OR(||) to AND(&&).

Shouldn't it work with OR(||)? Aren't I just saying that any of those two answers would be fine and the alert can run?

I know that this is very basic stuff and this may and up being a dumb mistake but I'm just a beginner trying to figure this out.

Thanks in advanced.

Answer:1

Your statement

answer != "yes" || answer != "yeah"

is true if either first or second condition is true. In your case regardless of the text in answer one of the conditions will always be true. For example:

  • if answer="yes" then second condition is true
  • if answer="yeah" then first condition is true
  • if answer="whatever" then both conditions are true

What you need to use instead is &&:

var answer = prompt("Are we there yet?");
while(answer != "yes" && answer != "yeah") {
    var answer = prompt("Are we there yet?");
}
alert("Yes! We made it!");

This will be true only when both conditions are met. For example:

  • if answer="yes" then first condition is false and second condition is true. Overall result is false and you leave the loop.
  • if answer="yeah" then first condition is true and second condition is false. Overall result is false and you leave the loop.
  • if answer="whatever" then both conditions are true and the loop is repeated.
Answer:2

Actually your condition checking is wrong.

here is your code snippet

 var answer = prompt("Are we there yet?");

 while(answer != "yes" || answer != "yeah") {
    var answer = prompt("Are we there yet?");
  }

  alert("Yes! We made it!");

In the above code snippet, while condition will always true whether it is "yes" or "yeah". you are binding two condition check with (||) OR Operator.

The (||) OR Operation is :-

true  || true = true
true  || false = true
false || true = true
false || false = false

So, (answar != "yes) || (answar != "yeah") will be true always either you type ("yes","yeah" or some other value).

The while loop will break only if condition return false but in above case it is always true. so it will create infinite loop.

lets try this :-

var answer = prompt("Are we there yet?");

while(answer != "yes" && answer != "yeah") {
    var answer = prompt("Are we there yet?");
}

alert("Yes! We made it!"); 

Due to (&&) AND Operation behavior,This will break infinite loop once any of value ("yes" or "yeah") given in prompt.

(&&) AND Operation behavior :-

true && false = false
false && true = false
false && false = false
true  && true = true 

So condition will be break once it contains any of value ("yes" or "yeah")

Answer:3

It needs to be an "and" because to get the loop, you need for the answer to be neither "yes" nor "yeah". If the tests were for equality rather than inequality, then you'd use the "or".

Answer:4

I like to use Javascript's split method to split a string like the one below into an array on every blank white line. How can I accomplish this. Splitting on \n doesn't because I'd like each ...

I like to use Javascript's split method to split a string like the one below into an array on every blank white line. How can I accomplish this. Splitting on \n doesn't because I'd like each ...

  1. split string blank space java
  2. split string blank space c#
  3. split string blank python
  4. split string blank
  5. string split blank space
  6. split string by blank space python
  7. powershell split string blank space
  8. java split string blank
  9. ruby split string blank
  10. r split string by blank
  11. bash split string by blank
  12. excel split string by blank
  13. powershell split string on blank line
  14. sql split string by blank
  15. php split string blank space

I have an object game = {}; and I save an array with two id's under every number of the object - game[number] = [id1, id2]; e.g. game[0] = [234342, 134313]. How can I get the number of the object game[...

I have an object game = {}; and I save an array with two id's under every number of the object - game[number] = [id1, id2]; e.g. game[0] = [234342, 134313]. How can I get the number of the object game[...

  1. array object value javascript
  2. array object values
  3. array object value php
  4. array object values java
  5. object array values c#
  6. array value object required
  7. object array value filter
  8. get array object value in javascript
  9. change array object value javascript
  10. array object find value javascript
  11. sum array object values javascript
  12. array object get value
  13. array object key value javascript
  14. get array object value in php
  15. array includes object value
  16. array object find value
  17. update array object value javascript
  18. array object change value
  19. array object key value
  20. javascript array object value exist

I'm building a large AngularJS application in which some of the UI text needs to be content managed. This is because items such as contextual help will need to be edited by the client after launch in ...

I'm building a large AngularJS application in which some of the UI text needs to be content managed. This is because items such as contextual help will need to be edited by the client after launch in ...

  1. content management string

I have a list var jsonList = [ { "id": "one", "pId": "nosearch", "cId": "searchc" }, { "id": "two", "pId": "product1", "cId": "searchc" }, { "...

I have a list var jsonList = [ { "id": "one", "pId": "nosearch", "cId": "searchc" }, { "id": "two", "pId": "product1", "cId": "searchc" }, { "...

  1. json list filter
  2. json list filter jquery
  3. json list filter angularjs
  4. python json list filter
  5. javascript json list filter
  6. json drop down list filter
  7. filter json list angular