-2

I am trying to compare two string using below code

var roomStatus = $("#room-status").val().trim();
var inService = "In Service";
var caInService = "CA In Service";

if (roomStatus !== inService || roomStatus !== caInService) 
{
  swal({
      title: "Allotment Cancelled",
      type: "error",
      text: "The Student cannot be alloted to this Room because this room is "+roomStatus,
  });
  return false;
}

The above code is not comparing if roomStatus equals In Service. It always falls in if condition. Please help !!!

treyBake
  • 6,440
  • 6
  • 26
  • 57
Nisa Nisa
  • 33
  • 6
  • Have you checked what roomStatus is returning and its type? Also it looks like you are looking to check if any of both strings match in order to raise the notice. – Dez Nov 14 '18 at 11:08
  • yes it is fetching i m debugging the code – Nisa Nisa Nov 14 '18 at 11:09
  • 10
    It's because you're using an OR condition. You need to invert your logic. – Rory McCrossan Nov 14 '18 at 11:11
  • Refer this link https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons – Mukesh Kalgude Nov 14 '18 at 11:11
  • 1
    actually i am checking if room status contains out of service or ca out of service, then it should raise an issue – Nisa Nisa Nov 14 '18 at 11:12
  • try this var roomStatus = $("#room-status").val().trim().toString(); – Mukesh Kalgude Nov 14 '18 at 11:16
  • in this code, no matter what you have in your roomservice, it will end up true.. If it is the one, it is not the other, if it is the other, it is not the one. no matter what you write, it will either not be `inService` or it will not be `caInService` - your logic is flawed – Stender Nov 14 '18 at 11:21
  • What exactly is the value of `roomStatus`? [This comment](https://stackoverflow.com/questions/53298772/not-comparing-strings#comment93478447_53298772) seems to very much muddy the waters – Liam Nov 14 '18 at 11:21
  • logic explanation : you check if it is NOT and apple, OR it is NOT a pear - everything in the world falls into this logic. - even pears and apples – Stender Nov 14 '18 at 11:28

2 Answers2

3

replace this line:

if (roomStatus !== inService || roomStatus !== caInService) {

with

if (!(roomStatus === inService || roomStatus === caInService)) {

Edit

as suggested by commenters, this line would be a lot easier to read:

if (roomStatus !== inService && roomStatus !== caInService) {
Ahmad
  • 12,336
  • 6
  • 48
  • 88
  • 4
    `if (roomStatus !== inService && roomStatus !== caInService)` is nicer, I think... (But that's a personal opinion, nothing objective) – Jean-Marc Zimmer Nov 14 '18 at 11:16
  • @Jean-MarcZimmer is right. I don't think it's personal preference either, the logic in this answer is just confusing – Liam Nov 14 '18 at 11:19
0

In an algorithm, there are two conditions groups that are pointless (with if (true) and if (false)) :

if (var !== val1 || var !== val2)

will always evaluate to true : if var is equal to val1, it's not equal to val2, and vice-versa.

if (var === val1 && var === val2)

will always evaluate to false, because var can't have two values at the same time.

note: Here I consider val1 !== val2 and simple conditions with no affectation

Jean-Marc Zimmer
  • 537
  • 6
  • 20