Ad
  • Custom User Avatar

    You are absolutely right. I have realized that my solution was wrong. Now I have refactored it and I have added some more test:

    vm = new VendingMachine({1:0, 3:2, 5:1, 8:0});
    Test.assertSimilar(vm.vending(9, {8:2}), {3:2});
    Test.assertSimilar(vm.coins, {1:0, 3:0, 5:1, 8:2});
    
    vm = new VendingMachine({20:0, 9:2, 5:2})
    Test.assertSimilar(vm.vending(1, {20:1}), {5:2,9:1});
    Test.assertSimilar(vm.coins, {5:0,9:1,20:1});
    

    Thanks a lot!

  • Custom User Avatar

    Hi.

    I think you can add another test, because my script has passed when it shouldn't.

    If you try this:

    var vm = new VendingMachine({1:0, 3:2, 5:1, 8:0});

    vm.vending(9, {8:2});

    As the price is 9 and I put 16 coins (8x2), the machine should return a change of 7, but it hasn't enough coins for that. According to the description, the machine should return the closest value to the expected change. In this case the closest value is 6 (3x2).

    As I mentioned, my script has passed the test cases, but if you try the example above, you'll see my script returns a change of 5 (5x1), so I suppose you can add a test case like this to ensure the scripts are well-constructed.

    Of course, I will improve my solution to pass this kind of test too.