countApplesAndOranges

    0

    0

    This snippet of code defines a function to count the number of apples and oranges in anarray.The function takes five parameters: (1) the number of apples to count, (2) the number of oranges to count, (3) the starting value for the apples count, (4) the starting value for the orange count, and (5) an array of strings that will be used to store the results.Each string in thearray will be replaced with the corresponding integer value.If thevalue in any string is greater than or equal to the number specified in thefunction call, that string's value is incremented byone, and the function moves on to the next string in thearray.If thevalue in any string is less than or equal to the number specified in thefunction call, that string's value is decremented byone, and the function moves on to the next string in thearray.Finally, thefunction calls thefunction, passing it thearray of strings and the number of strings in thearray.

    function countApplesAndOranges(s, t, a, b, apples, oranges) {
      let appleCount = 0, orangeCount = 0, p1 = 0, p2 = 0;
      while (p1 < apples.length || p2 < oranges.length) {
        if (apples[p1] >= 0) {
          let appleVal = a + apples[p1];
          if (appleVal >= s && appleVal <= t) {
            appleCount += 1
            p1 += 1;
          } else {
            p1 += 1
          }
        } else {
          p1 += 1;
        }
        if (oranges[p2] < 0) {
          let orangeVal = b + oranges[p2];
          if (orangeVal >= s && orangeVal <= t) {
            orangeCount += 1;
            p2 += 1;
          } else {
            p2 += 1;
          }
        } else {
          p2 += 1;
        }
      }
    
      console.log(appleCount);
      console.log(orangeCount);
    
    }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.