Jamie Doig
Jamie Doig

Reputation: 21

How to create a special OR statement in JavaScript

I'm trying to create a program where it keeps asking you for your first, middle and last name in a loop until you enter 'EXIT' or 'exit' as the last name. The || statement operator I'm using here isn't working and I can't seem to find the answer. Any ideas?

var FirstName = '' ;
var MiddleName = '' ;
var LastName = '' ;
var FullName = '' ;

while (LastName != 'EXIT' || LastName != 'exit' ) {

    FirstName = prompt ('What is your first name');
    MiddleName = prompt ('What is your middle name');
    LastName = prompt ('What is your last name');

    FullName = FirstName + ' ' + MiddleName + ' ' + LastName;

    alert ('Welcome, ' + FullName);
}

Cheers :)

Upvotes: 2

Views: 59

Answers (1)

Glenn Ferrie
Glenn Ferrie

Reputation: 10390

you need to check your boolean logic. here is a working code sample:

var FirstName = '' ;
var MiddleName = '' ;
var LastName = '' ;
var FullName = '' ;

while (LastName != 'EXIT' && LastName != 'exit' ) { /* updated */

    FirstName = prompt ('What is your first name');
    MiddleName = prompt ('What is your middle name');
    LastName = prompt ('What is your last name');

    FullName = FirstName + ' ' + MiddleName + ' ' + LastName;

    alert ('Welcome, ' + FullName);
}

!A AND !B instead of !A OR !B -- the latter is always true in your case because you are comparing the same variable to two different strings.

Upvotes: 2

Related Questions