How to Build a Model Train Calculator the Right Wayby@madsphi
1,597 reads

How to Build a Model Train Calculator the Right Way

August 25th 2022
4 min
by @madsphi 1,597 reads
tldt arrow
Read on Terminal Reader

Too Long; Didn't Read

A calculator where you can enter various factors like layout dimensions, scale, number of locomotives etc. and get a rough estimate of how much your new layout is going to cost you. Most of the calculator was written in Javascript, while the actual cost calculation was done by calling an API function. The calculation function has been written as an extension to the WordPress REST API. With the database table, I can also assign each calculation a unique id - with the id, people can share their calculations and tweak their calculation later on.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - How to Build a Model Train Calculator the Right Way
Mads Phikamphon HackerNoon profile picture

@madsphi

Mads Phikamphon

About @madsphi
LEARN MORE ABOUT @MADSPHI'S EXPERTISE AND PLACE ON THE INTERNET.
react to story with heart

If you are into model trains like me (I run the website Model Train Prices), you will know that things quickly become expensive. Much more expensive than you might expect when you start out.


To avoid such bad surprises, I decided to make a model train layout cost calculator. A calculator where you can enter various factors like layout dimensions, scale, number of locomotives etc. and get a rough estimate of how much your new layout is going to cost you.


Gathering requirements

I looked around, but couldn't find any existing model train layout cost calculators. All I could find were some blog posts talking about the costs and giving very different estimates of the cost. Some would say $30 per sq.ft. while others would say $100+ per square feet.


Having no clear consensus on costs and thinking like a pro developer, I decided to gather requirements before I started programming my new calculator.


In a few Facebook groups for model trains, I wrote about my planned calculator and asked what people thought about the factors and numbers I planned to use. Two of these posts got 20+ comments, which made me quite sure there was a need for such a calculator - and it also gave me a lot of input on how I should design the calculator.


image

The Javascript part

Having gathered the requirements, I knew what I had to make and could start the actual programming (i.e. start the best part). Most of the calculator was written in Javascript, while the actual cost calculation was done by calling an API function.


Writing the calculator in Javascript meant that it was easy to do validations before the numbers were submitted to the API and the Javascript could also take of currency conversions (which is important since my website targets people in multiple countries).


One of the validations the Javascript do is to validate that the total of the 3 environment percentages below is always 100%. The Javascript also updates how many percentages are missing if the total is below 100%.


image

The API cost calculation

The actual cost calculation could easily have been done by Javascript too, but instead, I choose to let it be done by an API function. There are two reasons for that:


  1. When the cost calculation is done by an API function, I can store the entered data in a database table, so I can see how people are using the calculator - and later on, when lots of people have used it, I can maybe write a blog post based on layout statistics (how many locomotives and wagons are people using for different sizes of layouts, what are the most popular layout sizes for different model scales, etc.)

  2. Using the database table, I can also assign each calculation a unique id. With the id, people can share their calculations and/or tweak their calculation later on.


Since my website is running on WordPress, the calculation function has been written as an extension to the WordPress REST API. Nice, easy and no need to setup a lot of things to get the API running. All I needed to do was to create an API class with my function in and add to the action 'rest_api_init'.


image

The id

As mentioned above, I assign each calculation a unique id. This is done by the calculation function, so when the function returns the results to the user, he can see a link with his id in.


image

Using a bit of Javascript, the current url is also changed to include the id.


image



Testing

To make sure everything is working correctly, I did a bunch of testing myself. But as most developers know, you are often blind to your own mistakes and might unconsciously avoid the dangerous parts of your code when testing.


I therefore also asked some friends to test things a bit, so I could be sure everything was working before starting marketing my calculator.


If you want to, you can also check out the finished calculator here.


Sharing it

The calculator is done, but unless you are coding something for your own personal use, code isn't worth much without anyone using it. It was therefore time to start spreading the word a bit.


Luckily I had already mentioned my calculator plans in some Facebook groups when I was gathering requirements. So why not return to those groups and share the finished calculator?


That way I could show people that I have actually implemented the thing we discussed - and hopefully, some of these people would check out the calculator, find it so useful and start using it.


image



RELATED STORIES

L O A D I N G
. . . comments & more!
Hackernoon hq - po box 2206, edwards, colorado 81632, usa