Temperature Converter
In this activity we will look at making an application together, step by step
Last updated
In this activity we will look at making an application together, step by step
Last updated
For this exercise, we are going to make a little application which takes an input value from the user in one of three units of temperature measurement (Celsius, Fahrenheit, or Kelvin) and converts it into one of the other two units of measurement.
Using Codepen (or your own environment, if you'd like) we are going to create the following simple layout
While there is nothing stopping you from copying and pasting the code from the examples (except maybe some accidental errors on my part), I highly encourage you to go through and type it out manually, and curiously question every line, to help understand what is going on.
To start things off, we are going to want to put our HTML elements on the screen so that we have something to work with. For the layout I am going to go with, I'm going to put down two input fields, two select boxes, and a button. Let's do that now:
This is what I am going to start with. This will give us just the basic elements we need to get something working, and we can always come back and make it pretty later. There are a few things going on here which we might not have covered yet, and I will touch on them briefly now.
Input type number - our two input
elements (the text fields) have an attribute on them called type
with a value of number
. What this does is tell the web browser that i want to make sure the use is only able to put numeric values in these fields. This saves us a bit of effort in validating the user's input (after all, we can't convert text into temperature)
Input placeholder - The placeholder attribute lets us specify what text to show to our users inside the field before they enter a value. This is a nice simple way to prompt the user with what type of value they should provide
select element - The select element lets us specify a number of option
elements inside it which then get displayed as dropdown selections for the user to choose pre-configured responses from
option element selected attribute - the selected
attribute lets us specify which options should be selected by default
Now that we have a basic layout to work with, we can start connecting some JS to our HTML. We are going to need to assign all 5 controls to variables, and make a function to run when we click our button. For now, we will just make the function copy the "from" value into the "to" field, without converting it. Once this is working, we can look at converting the value.
To do this, I am going to use document.getElementById
to target my 5 elements, and store them inside variables, like so:
Next, let's make that function. For now, just something simple to get the data flowing. I will make and use the function like this:
Now, if we enter some numbers into our "from" field and then click the convert button, the value will be written into the "to" field.
Now, time for the fun stuff.
The next thing we need to work on is the functions to actually convert between units of measurement. There are many different ways we could do this. The approach I am going to take is to convert Fahrenheit and Kelvin values into Celsius, and from there, convert it into the desired unit. At most this will take 2 steps, but it will reduce the number of different functions we need, as instead of handling from each unit, to each other unit (3 x 3 = 9) we only need to do from Fahrenheit and Kelvin to Celsius, and back again (only 4 functions). The formulas that we need to make
Let's implement these two functions now:
Now, we will also implement the inverse functions to convert from these units to celsius.
With these four functions, we should now be able to convert from any unit, to any other (via a few steps, if necessary)
Next, we need to use those select
elements to determine which operations to perform on the value, and convert the value before inserting it into the output field. To get the value out of the select elements, we can use a couple of DOM API properties. Specifically, select elements have an options
property which will give us an array of its options as DOM nodes, and a selectedIndex
property, which will return an integer signifying the index of the selected option. by combining these, we can get the currently selected option element, and extract its value
property. Doing so would look something like this:
Don't include this in your code, it is just an example of how to access items from a select element.
Using this, let's make a pair of functions. One for getting the user's input converted to celsius, and one for converting that celsius value to whatever the user has selected. These will make use of the functions we made above.
Now, we can use these two new functions to orchestrate the conversion for our user. To do this, lets replace our original conversion function with this:
This will do the following:
get the user's provided value, converted to celsius
get the desired unit to convert to
convert the celsius value to the desired unit
replace the value in the output field with this new converted value.
That's it. Give it a go. There are still a few things which can be improved. For example, some conversions will result in long numbers with lots of trailing decimal places, and it could do with some CSS to make it pretty, but the fundamentals are there.