[Chartjs]-Is there anything wrong with this If Condition in Javascript code?

1👍

the “other” condition inside the for loop triggers each time you iterate through the data, and all if conditions inside the loop fails.
So if you can push the “other” to the objects after for loop, each chart will contain one “other”.

Can you please correct me if my understanding is wrong?

for(var i in data)
        {
            //Gender.push("Gender " + data[i].JenisKelaminID);
            if(data[i].JenisKelaminID == 1)
            {
                Gender.push("Men");
            } 
            if(data[i].JenisKelaminID == 2)
            {
                Gender.push("Women");
            }

            jumlah.push(data[i].jumlah);

            if(data[i].Fakultas == "A")
            {
                Fakultas.push("FAPERTA");
            }

            if(data[i].Fakultas == "B")
            {
                Fakultas.push("FKH");
            }

            if(data[i].Fakultas == "C")
            {
                Fakultas.push("FPIK");
            }

            if(data[i].Fakultas == "D")
            {
                Fakultas.push("FAPET");
            }

            if(data[i].Fakultas == "E")
            {
                Fakultas.push("FAHUTAN");
            }

            if(data[i].Fakultas == "F")
            {
                Fakultas.push("FATETA");
            }

            if(data[i].Fakultas == "G")
            {
                Fakultas.push("FMIPA");
            }

            if(data[i].Fakultas == "H")
            {
                Fakultas.push("FEM");
            }

            if(data[i].Fakultas == "I")
            {
                Fakultas.push("FEMA");
            }

            jumlah_orang.push(data[i].jumlah_orang);
        }
        Gender.push("Other");
        Fakultas.push("Other");

1👍

From your descripion, I think it can be solved using else if as shown below.

for (var i in data) {
  //Gender.push("Gender " + data[i].JenisKelaminID);
  if (data[i].JenisKelaminID == 1) {
    Gender.push("Men");
  }else if (data[i].JenisKelaminID == 2) {
    Gender.push("Women");
  } else {
    Gender.push("Other");
  }

  jumlah.push(data[i].jumlah);

  if (data[i].Fakultas == "A") {
    Fakultas.push("FAPERTA");
  }else if (data[i].Fakultas == "B") {
    Fakultas.push("FKH");
  }else if (data[i].Fakultas == "C") {
    Fakultas.push("FPIK");
  }else if (data[i].Fakultas == "D") {
    Fakultas.push("FAPET");
  }else if (data[i].Fakultas == "E") {
    Fakultas.push("FAHUTAN");
  }else if (data[i].Fakultas == "F") {
    Fakultas.push("FATETA");
  }else if (data[i].Fakultas == "G") {
    Fakultas.push("FMIPA");
  }else if (data[i].Fakultas == "H") {
    Fakultas.push("FEM");
  }else if (data[i].Fakultas == "I") {
    Fakultas.push("FEMA");
  } else {
    Fakultas.push("Other");
  }

  jumlah_orang.push(data[i].jumlah_orang);
}

Or if A to I are constants you can do like this also

var mapping = {
  "A" : "FAPERTA",
  "B" : "FKH",
  "C" : "FPIK",
  "D" : "FAPET",
  "E" : "FAHUTAN",
  "F" : "FATETA",
  "G" : "FMIPA",
  "H" : "FEM",
  "I" : "FEMA"
};

for (var i in data) {
  //Gender.push("Gender " + data[i].JenisKelaminID);
  if (data[i].JenisKelaminID == 1) {
    Gender.push("Men");
  }else if (data[i].JenisKelaminID == 2) {
    Gender.push("Women");
  } else {
    Gender.push("Other");
  }

  jumlah.push(data[i].jumlah);

  var value = mapping[data[i].Fakultas];
  if(value){
    Fakultas.push(value);
  }else{
    Fakultas.push("Other");
  }

  jumlah_orang.push(data[i].jumlah_orang);
}

Leave a comment