0

I have an array which is to be iterated and depending on the condition execute task

I have done this with if else and trying it out with Switch.And the condition is if (1 && 2) (then execute A) else if (1) (then execute B) else if (2) (then execute c) else if (none) (then execute D)

function showFiletRelateddata(selectedFilter) {
  /*if (selectedFilter.length === 0) {
    console.log("No data");
  } else if (
    selectedFilter.includes("Request") &&
    selectedFilter.includes("Reservation")
  ) {
    console.log("RequestReservation");
  } else if (selectedFilter.includes("Request")) {
    console.log("Request");
  } else if (selectedFilter.includes("Reservation")) {
    console.log("Reservation");
  }*/

  var filt = selectedFilter;
  for (var i = 0; i < filt.length; i++) {
    var supp = filt[i];
    switch (supp) {
      case "Request":
      case "Reservation":
        console.log("RequestReservation");
        break;
      case "Request":
        console.log("Request");
        break;
      case "Reservation":
        console.log("Reservation");
        break;
      default:
        console.log("No data");
    }
  }
}

The if else is working fine however what correction needs to be made for Switch statement

For ref = Javascript switch case with array or strings

Enthu
  • 452
  • 1
  • 11
  • 34

1 Answers1

1

function showFiletRelateddata(selectedFilter) {
  var filt = selectedFilter;
  var supp = ""
  for (var i = 0; i < filt.length; i++) { //loop over length of array
     supp = supp + filt[i]; // concat elements of array
  }
  
    switch (supp) {
      case "RequestReservation": // if case contains one of the condition
      case "ReservationRequest":
        console.log("RequestReservation");
        break;
      case "Request":
        console.log("Request");
        break;
      case "Reservation":
        console.log("Reservation");
        break;
      default:
        console.log("No data");
    }
  
}
var a = ["Reservation", "Request"];
var b = ["Request","Reservation"];
var c = ["Reservation"];
var d = ["Request"];
 showFiletRelateddata(a);
 showFiletRelateddata(b);
 showFiletRelateddata(c);
 showFiletRelateddata(d);
Syed Mehtab Hassan
  • 1,222
  • 1
  • 8
  • 22
  • thanks, I have 2 queries 1. var supp you have used so var 2 time while declaring and then again var inside for (please explain) 2. In The if else condition which I had used include so that approach is correct or not – Enthu Apr 15 '19 at 10:29
  • 1.i have be using var supp = "" because if i only declare it inside loop the first time its value will be undefined which will be concat to the string. i have changed that was a mistake – Syed Mehtab Hassan Apr 15 '19 at 10:31
  • 2. it is correct you can also use indexOf instead of include – Syed Mehtab Hassan Apr 15 '19 at 10:32
  • ok thanks , and for var usage(point 1) please explain , thank you very much this runs like charm, I will accept this answer – Enthu Apr 15 '19 at 10:34
  • @Enthu if i donot declare var supp outside the loop the it will concate an undefined inside that because first time it is undefined supp = supp + filt[i] = undefined + filt[i]; – Syed Mehtab Hassan Apr 15 '19 at 10:37
  • I am a bit confused, u have updated and removed var from inside for, that was a typo I guess right :), I was confused due to that – Enthu Apr 15 '19 at 10:38
  • @Enthu yeah that what i said in the previous comment it was a mistake – Syed Mehtab Hassan Apr 15 '19 at 10:39