Codewars
Last updated
Last updated
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:
writing JavaScript ๐ป
breaking down problems ๐งฉ
recognising common questions ๐ต๏ธโโ๏ธ
building up your JavaScript muscles ๐ช
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
#cyf-codewars on Slack
Join the channel now!
Weโve made collections of kata and synced them to the Syllabus.
Here are the CYF collections:
https://www.codewars.com/users/CodeYourFuture/authored_collections
To edit these collections, use the pinned thread in #cyf-codewars channel.
First, click this link to view all of the CodeYourFuture collections.
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.
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?
I need to figure out how many milliseconds there are in each of these values
Convert the values to that number
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.
Clan name: CodeYourFuture Slack Channel: #cyf-codewars Collections: CYF Modules Further reading...