JavaScript - Mixed Juices - Problem

I’m not able to finish the Mixed Juices Exercise. Firefox and MS Edge are showing me notifications that the page is slowing the browser down.
Not sure if it is my code or the website…

If you share your code, someone might be able to help out!

Thanks for your reply Isaac!
This is actually not about my code.
When I click Run Tests the browser tab seems to crash.
I just tested this on Windows in Firefox & Edge and on macOS in Firefox and Safari.

Anyway here ist the code I have written:

// @ts-check
//
// The line above enables type checking for this file. Various IDEs interpret
// the @ts-check directive. It will give you helpful autocompletion when
// implementing this exercise.

/**
 * Determines how long it takes to prepare a certain juice.
 *
 * @param {string} name
 * @returns {number} time in minutes
 */
export function timeToMixJuice(name) {
  if (name === 'Pure Strawberry Joy') {
    return 0.5;
  } else if (name === 'Energizer' || name === 'Green Garden') {
    return 1.5;
  } else if (name === 'Tropical Island') {
    return 3;
  } else if (name === 'All or Nothing') {
    return 5;
  } else {
    return 2.5;
  }
}

/**
 * Calculates the number of limes that need to be cut
 * to reach a certain supply.
 *
 * @param {number} wedgesNeeded
 * @param {string[]} limes
 * @returns {number} number of limes cut
 */
export function limesToCut(wedgesNeeded, limes) {
  let numberOfLimes = 0;
  let numberOfWedges = 0;
  let i = 0;
  do {
    switch (limes[i]) {
      case 'small':
          wedgesNeeded -= 6;
          break;
        case 'medium':
          wedgesNeeded -= 8;
          break;
        case 'large':
          wedgesNeeded -= 10;
          break;
    }
    numberOfLimes++;
    i++;
  } while (wedgesNeeded > 0)
  return numberOfLimes;
}

/**
 * Determines which juices still need to be prepared after the end of the shift.
 *
 * @param {number} timeLeft
 * @param {string[]} orders
 * @returns {string[]} remaining orders after the time is up
 */
export function remainingOrders(timeLeft, orders) {
  throw new Error('Remove this line and implement the function');
}

You don’t guard against out-of-bounds access in limesToCut. If there is a test case where the limes are not enough to get all needed wedges, then i will eventually be >= the length of limes and limes[i] will be undefined.

Since you don’t have a default in your switch to handle invalid values, no case will run and the loop will keep going forever. This is likely what’s crashing your browser.

1 Like