Codewars is a site that has thousands of programming problems you can use to practice what you’ve learned in class.
On Codewars these problems are called Kata. A Kata is a little workout. Solving lots of kata gives you lots of practice in:
- breaking down problems 🧩
- recognising common questions 🕵️♀️
To sign up to Codewars visit this link and complete their simple tutorial
After you've signed in you'll want to go to "Account Settings" at the top right of the screen.
Codewars Clans are teams so that we can track how many challenges we've completed compared to other people. It also helps us share our solutions when we've completed them.
To join the CodeYourFuture clan you should enter "CodeYourFuture" as you see in the screenshot below
Make sure you scroll to the bottom of the page and click "Update" to save.
We've got a community of other CYF Trainees also working on Codewars exercises. You can find them at
Join the channel now!
We’ve made collections of kata and synced them to the Syllabus.
Here are the CYF collections:
To edit these collections, use the pinned thread in #cyf-codewars channel.
Choose a collection by clicking the title. You will see the collection view. If you click the green button marked ‘Train Now’, you’ll cycle through all the kata in this collection. Or you can go right to one you find interesting by clicking the title.
The first thing you’ll see is the details page - it will describe the problem. Sometimes these descriptions are very clear, but more often you will have to figure out what the kata wants by progressively working through with tests. And that’s what we want you to do.
You can read the description before you go into the kata to decide if you want to attempt it.
Okay, so this one wants us to calculate the number of milliseconds given in hours, minutes and seconds.
What else does this kata description give us? They don’t all give you the same things, but here are some common clues you might get.
Input constraints / limits of each value
They are saying that there aren’t 61 minutes, or 135 minutes in an hour, or a million hours in a day. We don’t need to handle those kinds of values in this kata. We can be sure it’s always going to follow the rules of the 24 hour clock.
A test case shows inputs and expected result
And here it gives us a test case -- here are inputs (h, m, s) and outputs (result) given. Let’s go into the kata by hitting Train.
And look, we can see the same values over here, in the tests panel.
The tests are showing inputs on the left and the expected output on the right. It’s saying the return value of the function past(), when given those inputs, must be equal to the value given after the comma.
The function past(0,1,1) must return the value 61000 for the test to pass.
So we could copy these answers into our code and just give the right outputs for the right inputs. We could even delete these other tests.
And look, green! But, would that solve the kata? No. If we change the inputs then the answers will be wrong. And the kata will do that for you. If you hit the blue “Attempt” button, it will run through loads more tests and look, now our answer doesn’t work any more.
So we can’t rewrite the tests to pass the code. We’ve got to rewrite the code to pass the tests.
Let’s work this problem
So I will reset by hitting the reset button and run the kata tests again.
Now my test is failing as undefined, because I’m not returning anything at all right now. I definitely need to return a value, so I’m going to write a return in. What other piece of information do I need?
- 1.I need to figure out how many milliseconds there are in each of these values
- 2.Convert the values to that number
- 3.Then add them together.
How many milliseconds are there in an hour, anyway? I’m gonna google that.
Oh look at that - google has a converter right there. Wow, that’s a big number! I’m going to copy and paste that and then hit test. Test often.
Hm, not right yet -- I guess I need to try doing the same thing for the minutes and the seconds and then adding them all together.
Oh look, success! But this is only a limited number of tests.
I’ll hit the blue attempt button (you can attempt over and over, and you should).
Now I’ve passed all my tests I can refactor and make it all tidy. Maybe I’ll make it a fat arrow function…
But look, it seems like I’ve solved this kata before so I’ve got my old solutions right here in this tab. What did I say?
That’s a pretty good point. I could one line this, and if I am practising one liners that is what I should do. But when I come back to this kata, I’m not convinced that I would know what these numbers mean. I wouldn’t know what 3.6e+6 meant, for sure. So maybe I should identify these values. I could do it the way I did last time, except I would spell milliseconds correctly. Hit submit.
And now we get to go and see how other people solved it. Click around the different filters. Look at all these different answers!
Ignore the “best practices” votes. The people voting are just learning and they don't know what best practices are. You will not write good code if you copy the best practices solutions on Codewars. But it’s interesting to see all the different ways to approach the problem - and there are always many many solutions.
(It’s worth repeating kata after a break - go back and train again on kata you’ve already done, a few weeks or months later and see how differently you approach things.)
So you need to:
- Read the description
- Run the tests
- Read the test output
- Google well and often
- Test and test again
- Refactor at the end
- Read others solutions
So that’s Codewars. Join the CodeYourFuture clan. Join the slack channel. Find the collections, and get some exercise.