Today's approach: Ruby
In this mini-project we are going to create a mini application in the terminal that converts the temperature from degrees kelvin to degrees fahrenheit. If you don't understand the difference between fahrenheit and kelvin that is ok. I'll share with you the formula as we walk through the steps.
I will list out the 6 steps below that you need to take to build out this mini-application. In the following video I will walk through all 6 steps myself so you can see a possible solution.
You can build out this application in any coding environment of your choice. If you are brand new to coding you may want to skip to the next video and watch the first couple minutes to see the coding environment I set up and would suggest you use.
Because this is our first mini-challenge you also may just want to watch me solve this challenge from start to finish to see how we solve these mini challenges then try the next mini challenge on your own. You can follow this link to find the completed code or check the bottom of this page.
Open up a new Repl to get started!
Challenge: Convert any given temperature in Kelvin to Fahrenheit
- Let's imagine that the weather reports says that the temperature today will be 301 Kelvin. How should you dress for the day? Let's create an app that lets us know the temperature in fahrenheit. To start, create a variable named
$kelvin_temp, and set it equal to
301. Write a comment above that explains this line of code.
- Finding the temperature in Celsius is similar to Kelvin — the only difference is that Celsius is 273.15 degrees less than Kelvin.
Let's convert Kelvin to Celsius by subtracting
$kelvin_tempvariable. Store the result in another variable, named
Write a comment above that explains this line of code.
- Use this equation to calculate Fahrenheit, then store the answer in a variable named
Fahrenheit = Celsius * (9/5) + 32
In the next step we will round the number saved to
$fahrenheit_temp. Write a comment above that explains this line of code.
- Log to the console the value of
$fahrenheit_temp. In our next step we are going to see what we can do to make sure that our number is a whole number by rounding down.
echothe value to the terminal. It should begin with
- As we have just seen, when you convert from Celsius to Fahrenheit, you often get a decimal number. Go ahead and delete the
echocode from step 4.
floor()method to round down the Fahrenheit temperature. Save the result to the
$fahrenheit_tempvariable. Write a comment above that explains this line of code.
echoand string concatenation to display the temperature in
$fahrenheit_tempto the terminal to create the message as follows:
The temperature is TEMPERATURE degrees Fahrenheit.
TEMPERATURE should be determined by the value of
Great job! I'll walk through these steps in the next video!
<?php // Temperature in kelvin stored in variable `$kelvin_temp` $kelvin_temp = 301; // temperature in celsius stored in variable `$celsius_temp` $celsius_temp = $kelvin_temp - 273.15; // convert celsius to fahrenheit stored in variable `$fahrenheit_temp` $fahrenheit_temp = $celsius_temp * (9/5) + 32; // round the value of fahrenheit down and assign to `$fahrenheit_temp` $fahrenheit_temp = floor($fahrenheit_temp); // use string concatenation to leave a message // "The temperature is `fahrenheit` degrees Fahrenheit." echo 'The temperature is ' . $fahrenheit_temp . ' degrees Fahrenheit.';