Buy Verified Paxful Account

Paxful is a popular peer-to-peer cryptocurrency marketplace that allows users to buy and sell Bitcoin. In order to buy Bitcoin on Paxful, you must first create an account. You can do this by…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Conditional Coin Flips with Rock Paper Scissors

In Perth I remember seeing a Heads or Tails game in their casino!

Let start by writing a test for it first. This is universally agreed upon of good practice to write tests first before writing code (aka TDD, Test Driven Development). It may seem strange, but this allows a developer to have a good process of thinking how they will write the code out before doing it.

A well tested program will also give the code an assurance of quality and reliability, for when things are added or changed, the tests will give out visibility on where the problems are.

Folder structure of Jasmine standalone. By default there are example files there.

The lib folder is where Jasmine is stored, so don’t worry about that. What we care about is the spec and src folder. The spec folder is where will will write our test, and the src folder is where our code will be. Then we will put both files in SpecRunner.html so that it will run the tests on your code in the browser by loading that up.

Opening up SpecRunner shows their default tests

Aight, lets create our own blank files for the test and the code and put them into the SpecRunner.html

Lets write our test!

Tests are written like this. The describe block is a function with the first parameter being a string describing the test. I typically just write #[function] to show which function it is talking about. The second parameter is another function it which does the same thing as the describe, but this time we write a description in more detail.

The second parameter of the it function is another function which will contain our code and the steps to create the outcome we want.

So for our function coinRPS we know that we wanna take in two parameters, one for the coin and one for the RPS which will be strings. We also know we want the outcome to return a number from 0 to 5.

Our function coinRPS gets called and assigned to result. Then we compare result’s value and see if that is zero.

It definitely is a super confusing structure with functions inside functions, but if you stick with it you’ll get used to it and hopefully it will be helpful!

Lets check out our test in the browser. Go to SpecRunner and hit refresh.

Test fails because coinRPS function doesn’t exist yet!

Ok cool. Let write some code in.

This will pass.

First passing test!

This process of writing a failing test, then making it pass is what is done for TDD. The next step would be to write one for returning 1. Each small step you take towards your final goal. But for the sake of time, I’ll write a test for all the outcomes.

As you can see there’s quite a bit of repetition here. In programming its good practice to keep code ‘DRY’ (Do not repeat). But for testing its ok, as the aim is to write quality tests first and foremost.

Lets check out some different ways of doing this;

Probably the most common way of doing this is just going for 6 if statements straight up;

It passes, enjoy that dopamine hit by seeing this screen on your SpecRunner!

It kinda looks similar to a switch statement, lets try that.

Well, that doesn’t actually look like its saved any space at all. In fact it might need a break after each return, which seems like a lot more code.

Hmmm that’s a lot of of repetition tho, how about using a kinda tree format?

This kinda loses *some* repetition? But what we’ve lost in repetition we seemed to have increased in curly brackets :/

Ok, I’ve heard ternary operators are good at losing brackets, lets try that? Ternaries have the format

[conditional statement] ? [result if true] : [result if false]

and I suspect that this will have to use nested ternaries which can be like

[conditional statement] ? [result if true] : [another conditional statement if false] ? [result if false then true] : [result if false then false]

Head hurting? Doesn’t look any less confusing…

But on the bright side there definitely is less brackets, so plenty of points for that! However, for readability I’d say its pretty low down the scale, and thank God I wrote that test to help me out!

Ok, maybe a different approach. Why not write out the outcomes and then match that? #ThinkingOutsideTheBox

Ok so here we got a hash table of outcomes. The input grabs the string from coin and the string from RPS, and adds a space in between them, just like the strings in the hash values.

Object.values(outcomes) loops through the values of our hash table outcomes, and if it matches with input it will assigns the index number to result, which is our number!

We can do a bit of refactoring with some ES6 template strings, and ternary’ing it up!

Template strings uses backticks ` ` and inside them you use ${} for variables, in this case we can use the parameters coin and RPS passed in.

But wait! You know what? Why not save more precious screen space by using an array instead!

indexOf is a method which loops through an array and if it finds a match returns the index position of it. Perfect for what we want to do here!

I think one of the toughest things in writing code is to work out the best balance between readability and cleverness. You want it to be code which is able to be understood by the majority of developers, because nearly all coders are working in a team.

People come and go, or maybe you might be returning to some of your own code that you wrote a year ago. I think the nested ternary solution is a good example as a clever solution, but really lacks in readability unless you work ternaries a lot, which probably isn’t the case with most developers.

However its so hard to get this balance right, and different people have different opinions on where they believe this line should be.

Add a comment

Related posts:

Quarantine day ?

From the very start of the COVID-19 outbreak, I like to think that many others shared similar concerns about what lies ahead of us. For some that meant stocking up pre-apocalyptic style, which…

An intro to the useful skills level 2

One of the most usual substitute for a GCSE in mathematics and English is the functional abilities level 2 certification, which approaches a grade C or degree 4. Capacities at Level 2 Practical…

Home Depot

A Comprehensive Guide to Home Improvement Projects for DIY Enthusiasts: Home Improvement 1–2–3 3rd Edition Book Home Improvement 1–2–3 3rd Edition is an essential guide for homeowners looking to…