dwyl / learn-tdd

:white_check_mark: A brief introduction to Test Driven Development (TDD) in JavaScript (Complete Beginner's Step-by-Step Tutorial)
Mozilla Public License 2.0
2.75k stars 348 forks source link

Own valid function in change.js causes error in blanket.js #99

Closed S4sh closed 7 years ago

S4sh commented 7 years ago

I wrote my own getChange function, but when clicking on "Enable covarage", chrome throws this error:

blanket.js:1161 Uncaught Error: Line 14: Unexpected token > at throwError (blanket.js:1161) at throwUnexpected (blanket.js:1218) at parsePrimaryExpression (blanket.js:1564) at blanket.js:3601 at trackLeftHandSideExpressionAllowCall (blanket.js:3496) at parsePostfixExpression (blanket.js:1700) at blanket.js:3601 at parseUnaryExpression (blanket.js:1780) at blanket.js:3601 at parseMultiplicativeExpression (blanket.js:1786)

When replacing my own with your function everything works fine.

My function:

function getChange(totalPayable, cashPaid)
{
  var coins = [200, 100, 50, 20, 10, 5, 2, 1];
  var changeToMake = cashPaid - totalPayable;
  var coinsToReturn = [];

  while (changeToMake !== 0)
  {
    coins.forEach(coin => {

      let coinCount = Math.floor(changeToMake/coin);

      if (coinCount >= 1)
      {
        for (let i = 0; i < coinCount; i++)
        {
          coinsToReturn.push(coin);
        }

        let remainder = changeToMake % coin;

        changeToMake = remainder;
      }
    });
  }
  return coinsToReturn;
}

SOLVED:

Writing this function, I found the problem (- not that I haven't tried before). Blanket.js does not accept ES6 arrow functions. So replacing (coin) => {...} with function(coin) {...} does the job.

So maybe you'd like to give a hint to others or use my function as another solution.

BTW: Great work! Your tutorials help me a lot and are perfectly written and understandable!

nelsonic commented 7 years ago

@S4sh thank you for opening this issue! 👍 Yeah, Blanket.js only allows JS that "works everywhere" ... Perhaps a couple of lines in the readme informing people to use the function keyword would be good. 🤔 Also, => are a nice convenience, however: https://plus.google.com/+DouglasCrockfordEsq/posts/TxQ4gRkZxST

Fl4m3Ph03n1x commented 7 years ago

@nelsonic To counter Douglas Crockford argument: https://medium.com/javascript-scene/familiarity-bias-is-holding-you-back-its-time-to-embrace-arrow-functions-3d37e1a9bb75

I believe arrow functions are a good thing, but that is my opinion. What would be your opinion of re-doing the tutorial using Mocha + chai, or some other more recent framework?

People (especially new generations), will use ES6. I think it only makes sense to write a tutorial that supports it - You don't have to use it, but you should at least support it.

Thoughts ?

S4sh commented 7 years ago

thank you @nelsonic

I think as a quick "workaround" it would be totally sufficient to give people a hint. For a "refactored" version of this tutorial, imho Mocha + chai (+ sinon) is seen as the tool of choice these days.

I also think that for future versions ES6 should be supported as it will work everywhere hopefully in the foreseeable future.

But, as said, for the time being, the tutorial fullfills it purpose very, very well.

nelsonic commented 7 years ago

@Fl4m3Ph03n1x, Eric's post is what drove me to consider fully functional programming languages (i.e. Elixir & Elm) and I have not looked back!