exercism / javascript-test-runner

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Tests timed out error on a working solution - Mixed Juices / JavaScript

yettyo opened this issue · comments

commented

I was trying to complete the Mixed Juices exercise on Javascript track and the solution I came put with seems to be working fine on VScode but when I try running tests on the website I'm getting the YOUR TESTS TIMED OUT error. My solution was:

1. export function timeToMixJuice(name) {
2.     switch(name) {
3.         case 'Pure Strawberry Joy':
4.             return 0.5;
5.         case 'Energizer':
6.             return 1.5;
7.         case 'Green Garden':
8.             return 1.5;
9.         case 'Tropical Island':
10.             return 3;
11.         case 'All or Nothing':
12.             return 5;
13.         default:
14.             return 2.5;
15.     }
16. }
17. 
18. export function limesToCut(wedgesNeeded, limes) {
19.     var wedgesCut = 0; var i = 0;
20.     do {
21.         switch (limes[i]) {
22.             case 'small':
23.                 wedgesCut += 6;
24.                 break;
25.             case 'medium':
26.                 wedgesCut += 8;
27.                 break;
28.             case 'large':
29.                 wedgesCut += 10;
30.                 break;
31.             default:
32.                 break;
33.         }
34.         i++;
35.     } while(wedgesCut < wedgesNeeded);
36.     return i;
37. }
38. 
39. export function remainingOrders(timeLeft, orders) {
40.     do {
41.         timeLeft -= timeToMixJuice(orders[0]);
42.         orders.splice(0,1);
43.     } while(timeLeft > 0);
44. 
45.     return orders;
46. }

I copy pasted the tests it's used to explain the problem in the instructions:

console.log(timeToMixJuice('Tropical Island'));
console.log(timeToMixJuice('Berries & Lime'));
console.log(limesToCut(25, ['small', 'small', 'large', 'medium', 'small']));
console.log(remainingOrders(5, ['Energizer', 'All or Nothing', 'Green Garden']));

And here's the output I get for running this file on VScode (Which is the same as in the instruction):

3
2.5
4
[ 'Green Garden' ]

If you check the code for limesToCut , what happens if there are no limes to cut, or not enough limes to cut? Here are the test cases for that:

test('uses up all limes if there are not enough to reach the target', () => {
  const limes = [
    'small',
    'large',
    'large',
    'medium',
    'small',
    'large',
    'large',
  ];

  expect(limesToCut(80, limes)).toBe(7);
});
test('works if no limes are available', () => {
  expect(limesToCut(10, [])).toBe(0);
});
commented

@SleeplessByte Thank you for pointing that out to me and sorry for wasting your time, I should have checked my code better before assuming that it was a test runner bug.

No worries. No time was wasted because your question was clear.

Have fun solving it!

commented

No worries. No time was wasted because your question was clear.

Have fun solving it!

Oh I already solved it 😅
I added line 4 to get rid of the "if there are no ___" cases. Thanks again 👍🏽.
Not having line 21 was causing my code to infinite loop.

1. function limesToCut(wedgesNeeded, limes) {
2.     var wedgesCut = 0; var i = 0;
3.     //return 0 if we don't need any lime wedges OR there's no limes to cut.
4.     if(limes.length == 0 || wedgesNeeded == 0) return i;
5.     do {
6.         switch (limes[i]) {
7.             case 'small':
8.                 wedgesCut += 6;
9.                 break;
10.             case 'medium':
11.                 wedgesCut += 8;
12.                 break;
13.             case 'large':
14.                 wedgesCut += 10;
15.                 break;
16.             default:
17.                 break;
18.         }
19.         i++;
20.         //return if there's no limes left to cut.
21.         if(i == limes.length) return i;
22.     } while(wedgesCut < wedgesNeeded);
23.     return i;
24. }