Ad
  • Custom User Avatar

    To write tests for your code you first need to refactor it into smaller test-able units. The existing function in your code contains user-interaction (alert(), prompt(), ...) which makes it impossible to test using a unit-test framework like codewars provides.

    I have done some refactoring and uploaded them as fork -> https://www.codewars.com/kumite/604243f85a72ae000d16e0bf?sel=60434735db87fd000c194e99

    I extracted functions for

    • determining which element wins when two elements (i.e. Rock and Scissors) are passed as parameters
    • determining which player wins (first or second parameter) when two elements are passed as parameters
    • determining a descriptive message for each round that
      • contains the correct verb for the winning element (i.e. crushes when Rock has won)
      • starts with a word describing who has won ("Your ...", "CPU ...")

    ... and also started to write some tests for the first function.

    I decided not to use the internal test framework from codewars but chose to use "Mocha BDD" instead (you can simply select it in your Kumite/Kata when designing a new one). I think it is more useful to learn how to use real-world test-frameworks when starting to learn about unit-tests. Mocha is often used in combination with an assertion framework - I have used "chai" in my case. You can find information about how both frameworks work and what they do here:

    https://mochajs.org/#getting-started
    https://mochajs.org/#interfaces
    https://www.chaijs.com/guide/styles/#assert

    Maybe it helps you as a starting point on how to write tests for your code ... and also on how to create Kumites/Katas for Codewars :)

  • Custom User Avatar

    Yep - it is confusing that the introduction says one should round down to the NEAREST integer - because the nearest integer of 25.6 is 26 which could only be achieved by rounding up ^^