Ad
  • Custom User Avatar

    Thank you Moormaster for your solution and links provided on test framework, just realized as u mentioned prompt and alert could not work on this site for reasons of ui specially when we need to apply the test code, and the test should provide input data and result, your description on extracted functions are nice and complete.

    Need a little time to read and understand your code (as i'm a newbie) after that I will reply this post again as im trying to make this challenge complete for submission on kata which now thanks to data you provided should be possible...

  • 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 ^^