Hash Tables: Ice Cream Parlor

  • + 0 comments

    JS - only using array

    function whatFlavors(cost, money) {
      // Write your code here
      for (let i = 0; i < cost.length; i++) {
        const flavor1 = cost[i];
        if (flavor1 < money) {
            // only check indexes after the current one (i)
            const idxFlavor2 = cost.indexOf(money - flavor1, i + 1);
            
            // check if "corresponding" price exists, if true: print and exit
            if (idxFlavor2 > i) {
                console.log(i + 1 + ' ' + (idxFlavor2 + 1));
                // we done, loop can be aborted
                return;
            }
        }
      }
    }