skip to Main Content

I am working on node js and mysql here i am using profile incomplete or complete logic so i have to check all the fields or empty or not so i have 28 fields i have used 28 if else can we make shorter code.

Here my code
so how we can reduce my code if we can reduce our code please share your solution.

Here no error this code is correct But I need to make shorter code it will be good.

let points = 0;
        let point1 = 0;
        let point2 = 0;
        let point3 = 0;
        let point4 = 0;
        let point5 = 0;
        let point6 = 0;
        let point7 = 0;
        let point8 = 0;
        let point9 = 0;
        let point10 = 0;
        let point11 = 0;
        let point12 = 0;
        let point13 = 0;
        let point14 = 0;
        let point15 = 0;
        let point16 = 0;
        let point17 = 0;
        let point18 = 0;
        let point19 = 0;
        let point20 = 0;
        let point21 = 0;
        let point22 = 0;
        let point23 = 0;
        let point24 = 0;
        let point25 = 0;
        let point26 = 0;

        if (profileRes[0].username == null || profileRes[0].username == "") {
            point1 = points + 10;
        } else if (profileRes[0].username != null || profileRes[0].username != "") {
            point1 = points += 0;
        }
        if (profileRes[0].phone == null || profileRes[0].phone == "") {
            point2 = points + 10;
        } else if (profileRes[0].phone != null || profileRes[0].phone != "") {
            point2 = points += 0;
        }
        if (profileRes[0].email == null || profileRes[0].email == "") {
            point3 = points + 10;
        } else if (profileRes[0].email != null || profileRes[0].email != "") {
            point3 = points += 0;
        }
        if (profileRes[0].company_name == null || profileRes[0].company_name == "") {
            point4 = points + 10;
        } else if (profileRes[0].company_name != null || profileRes[0].company_name != "") {
            point4 = points += 0;
        }
        if (profileRes[0].profile_description == null || profileRes[0].profile_description == "") {
            point5 = points + 10;
        } else if (profileRes[0].profile_description != null || profileRes[0].profile_description != "") {
            point5 = points += 0;
        }
        if (profileRes[0].short_description == null || profileRes[0].short_description == "") {
            point6 = points + 10;
        } else if (profileRes[0].short_description != null || profileRes[0].short_description != "") {
            point6 = points += 0;

        }
        if (profileRes[0].company_url == null || profileRes[0].company_url == "") {
            point7 = points + 10;
        } else if (profileRes[0].company_url != null || profileRes[0].company_url != "") {
            point7 = points += 0;

        }
        if (profileRes[0].company_linkedin == null || profileRes[0].company_linkedin == "") {
            point8 = points + 10;
        } else if (profileRes[0].company_linkedin != null || profileRes[0].company_linkedin != "") {
            point8 = points += 0;

        }
        if (profileRes[0].company_instagram == null || profileRes[0].company_instagram == "") {
            point9 = points + 10;
        } else if (profileRes[0].company_instagram != null || profileRes[0].company_instagram != "") {
            point9 = points += 0;

        }
        if (profileRes[0].company_facebook == null || profileRes[0].company_facebook == "") {
            point10 = points + 10;
        } else if (profileRes[0].company_facebook != null || profileRes[0].company_facebook != "") {
            point10 = points += 0;

        }
        if (profileRes[0].number_of_employees == null || profileRes[0].number_of_employees == "") {
            point11 = points + 10;
        } else if (profileRes[0].number_of_employees != null || profileRes[0].number_of_employees != "") {
            point11 = points += 0;

        }
        if (profileRes[0].company_logo == null || profileRes[0].company_logo == "") {
            point12 = points + 10;
        } else if (profileRes[0].company_logo != null || profileRes[0].company_logo != "") {
            point12 = points += 0;

        }
        if (profileRes[0].founding_year == null || profileRes[0].founding_year == "") {
            point13 = points + 10;
        } else if (profileRes[0].founding_year != null || profileRes[0].founding_year != "") {
            point13 = points += 0;

        }
        if (profileRes[0].is_profile_published == null || profileRes[0].is_profile_published == "") {
            point14 = points + 10;
        } else if (profileRes[0].is_profile_published != null || profileRes[0].is_profile_published != "") {
            point14 = points += 0;

        }
        if (profileRes[0].legal_name == null || profileRes[0].legal_name == "") {
            point15 = points + 10;
        } else if (profileRes[0].legal_name != null || profileRes[0].legal_name != "") {
            point15 = points += 0;

        }
        if (profileRes[0].billing_address == null || profileRes[0].billing_address == "") {
            point16 = points + 10;
        } else if (profileRes[0].billing_address != null || profileRes[0].billing_address != "") {
            point16 = points += 0;

        }
        if (profileRes[0].zip_code == null || profileRes[0].zip_code == "") {
            point17 = points + 10;
        } else if (profileRes[0].zip_code != null || profileRes[0].zip_code != "") {
            point17 = points += 0;

        }
        if (profileRes[0].vat_number == null || profileRes[0].vat_number == "") {
            point18 = points + 10;
        } else if (profileRes[0].vat_number != null || profileRes[0].vat_number != "") {
            point18 = points += 0;

        }
        if (profileRes[0].sdi_codex == null || profileRes[0].sdi_codex == "") {
            point19 = points + 10;
        } else if (profileRes[0].sdi_codex != null || profileRes[0].sdi_codex != "") {
            point19 = points += 0;

        }
        if (profileRes[0].benefits == null || profileRes[0].benefits == "") {
            point20 = points + 10;
        } else if (profileRes[0].benefits != null || profileRes[0].benefits != "") {
            point20 = points += 0;

        }
        if (profileRes[0].category == null || profileRes[0].category == "") {
            point21 = points + 10;
        } else if (profileRes[0].category != null || profileRes[0].category != "") {
            point21 = points += 0;

        }
        if (profileRes[0].branch == null || profileRes[0].branch == "") {
            point22 = points + 10;
        } else if (profileRes[0].branch != null || profileRes[0].branch != "") {
            point22 = points += 0;

        }
        if (profileRes[0].coreActivities == null || profileRes[0].coreActivities == "") {
            point23 = points + 10;
        } else if (profileRes[0].coreActivities != null || profileRes[0].coreActivities != "") {
            point23 = points += 0;

        }
        if (profileRes[0].districts == null || profileRes[0].districts == "") {
            point24 = points + 10;
        } else if (profileRes[0].districts != null || profileRes[0].districts != "") {
            point24 = points += 0;

        }
        if (profileRes[0].legalFormCompany == null || profileRes[0].legalFormCompany == "") {
            point25 = points + 10;
        } else if (profileRes[0].legalFormCompany != null || profileRes[0].legalFormCompany != "") {
            point25 = points += 0;

        }
        if (profileRes[0].commune == null || profileRes[0].commune == "") {
            point26 = points + 10;
        } else if (profileRes[0].commune != null || profileRes[0].commune != "") {
            point26 = points += 0;

        }
        else {

        
        }
        let pint = point1 + point2 + point3 + point4 + point5 + point6 + point7 + point8 + point9 + point10 +point11 + point12 + point13 + point14 + point15 + point16 + point17 + point18 + point19 + point20 + point21 + point22 + point23 + point24 + point25 + point26;

        let percentage = (pint * 100) / 260;

2

Answers


  1. Should all keys in the profileRes[0] have a corresponding points variable? If yes, then something like this might work. This solution will put all of your points into one object instead of a list of variables:

    const pointsMap = Object.entries(profileRes[0]).reduce((acc, [key, value]) => {
        acc[key] = !!value ? 0 : 10
        return acc
    }, {})
    
    // returns an object that has key/value pairs accessible by the profileRes[0] keys
    // for example, {username: 10, email: 0, phone: 10...}
    

    Note, the above answer uses !!value to determine if the value is truthy. This means values like null and "" will fail. undefined will also fail this check. If a key in profileRes[0] can be undefined and undefined should be considered a valid value, the line can change to be the following instead:

    acc[key] = (value === null || value === "") ? 10 : 0
    
    Login or Signup to reply.
  2. You can remove all the points variables

    let points = 0;
    
    for (let key in profileRes[0]) {
      if (profileRes[0][key] === null || profileRes[0][key] === '') {
        points += 10;
      } else {
        points += 0;
      }
    }
    let percentage = (points * 100) / 260;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search