skip to Main Content

I am trying to call two functions "company1Qualified()" & "company2Qualified()" if the corresponding id is in bookable companies variable. My issue is that I can not call both functions at the same time with my condition. Only one function will get called at the moment. The first id in bookableCompanies will always be called but it fails to call both functions. What is causing this? How can I get this to call both functions when the ids are in bookableCompaines?

I created a test environment below to run this code.

    <!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Test setQualified</title>
  </head>
  <body>
    <h1>Test setQualified</h1>

    <button onclick="setQualified()">setQualified</button>
  </body>

  <script>
    function company1Qualified() {
      console.log("Call company1Qualified");
    }

    function company2Qualified() {
      console.log("Call company2Qualified");
    }

    function setQualified() {
      const company1Id = "0011U00002LNeJ9QAL";
      const company2Id = "0011U00002BkHdaQAF";
      let iscompany1Q = "";
      let iscompany2Q = "";
      // let iscompany1Q = company1Qualified();
      // let iscompany2Q = company2Qualified();

      // Get List of Bookable Companies.
      let bookableCompanies =
        "0011U00002LNeJ9QAL, 0011U00002BkHdaQAF, a1FKi000000PBG8MAO, 0011U00002GhNOXQA3";
      var companiesArray = bookableCompanies.split(",");

      // Check if company1Id and company2Id are in bookableCompanies
      if (
        companiesArray.includes(company1Id) &&
        companiesArray.includes(company2Id)
      ) {
        iscompany1Q = company1Qualified();
        iscompany2Q = company2Qualified();
      } else if (companiesArray.includes(company2Id)) {
        iscompany2Q = company2Qualified();
      } else if (companiesArray.includes(company1Id)) {
        iscompany1Q = company1Qualified();
      }

      const qualifiedCompanyValue =
        iscompany2Q && iscompany1Q
          ? `${company1Id}, ${company2Id}`
          : iscompany2Q
          ? company2Id
          : iscompany1Q
          ? company1Id
          : "";

      console.log("Qualified Companies:", qualifiedCompanyValue);
    }
  </script>
</html>

2

Answers


  1. When you do this:

          // Get List of Bookable Companies.
      let bookableCompanies =
        "0011U00002LNeJ9QAL, 0011U00002BkHdaQAF, a1FKi000000PBG8MAO, 0011U00002GhNOXQA3";
      var companiesArray = bookableCompanies.split(",");
    

    You get the following array:

    ["0011U00002LNeJ9QAL", " 0011U00002BkHdaQAF", " a1FKi000000PBG8MAO", " 0011U00002GhNOXQA3"]
    

    Note the leading spaces in all but the first element. This prevents the companiesArray.includes(company2Id) condition from passing.

    Instead, either re-format the string to not include spaces, change the split to bookableCompanies.split(", "), or strip the whitespace from each element of the array.

    Login or Signup to reply.
  2. As mentioned the split leaves a leading space that breaks includes().

    I recommend the following format to break anything into words:

    const companiesArray = "0011U00002LNeJ9QAL 0011U00002BkHdaQAF a1FKi000000PBG8MAO 0011U00002GhNOXQA3".match(/S+/g);
    
    console.log(companiesArray)

    The logic could be simplified, since the result will be the same, no need for the double check:

    let iscompany1Q = companiesArray.includes(company1Id) ? company1Qualified() : '';
    let iscompany2Q = companiesArray.includes(company2Id) ? company2Qualified() : '';
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search