Lua Task 3 - Calculations and tests
editPrerequisite: Lua Task 2 - Working with modules. This task requires some independent learning and is more difficult.
Calculations
editIn a Lua function, we can do the usual programming jobs, such as calculations, text processing, testing and repeating blocks of code. You'll need to read Lua Reference Manual if you are uncertain about anything.
1. In your module sandbox, create a new function called converttemp
. In the call, we will pass a parameter named celsius
and store it inside the function as a variable named cels
. We will then convert it to degrees Fahrenheit and store it in a variable named fahr
. Finally, we will return both values along with some text.
Use the line
cels = frame.args.celsius
to get the parameter |celsius=
and store it in the variable cels
.
2. To convert to degrees Fahrenheit, we multiply a value representing the temperature in degrees Celsius by 9, then divide it by 5, and add 32. Write a line starting
fahr =
to do that conversion. You'll need the multiplication (*), division (/), and addition (+) operators.
3. Write a line of code that returns something like 15 degrees Celsius is 59 degrees Fahrenheit. when the function is passed 15 as the |celsius=
parameter. You'll need the concatenation (..) operator that puts strings together. Save your module sandbox.
4. In your user sandbox, add a new heading at the end by typing == Task 3 ==
5. Write a new line that calls your module sandbox function converttemp
, passing 5 as the parameter.
6. Leave a blank line, then write another line that calls the function with 25 as the parameter. Save your sandbox. Fix any errors that occur.
Tests
editNote that a Scribunto function always returns a string (i.e. text) to the Wikipedia page where it is invoked and parameters passed to the module are always strings (or nil). Lua variables are dynamically-typed. That means that variables do not have a type, only values have a type. Lua will generally do its best to convert from one type to another as needed. For example, you can concatenate text with numbers without having to explicitly convert the number to a string first. As you saw in the previous part, you can do arithmetic with strings that can be treated as numbers. However, when testing numbers, you must make sure that you've explicitly converted any parameters (which are all strings) to numbers first, otherwise the test will fail because Lua can't be certain whether to change the number to a string or change the string to a number in order to do the test. For example: 9 < 10
(true for numbers) is different from '9' < '10'
(false for strings).
Lua uses the if <test> then <dothis-if-true> else <dothis-if-false> end
structure to pick one of two outcomes depending on whether the test turns out true or false.
7. In your module sandbox, amend the function to supply 0
as the default if no parameter is supplied. Save it.
8. Write a line in your user sandbox to test that. Save it.
9. Make changes to the function converttemp
to store the text that you are returning in a variable called msg
. Then return msg
as the last line before the end
of the function. Save it and check in your user sandbox that it is still working (refresh the page or preview an edit).
10. Change function converttemp
to explicitly convert the variable cels
to a number. You'll need the tonumber()
function built in to Lua. Save it and check.
11. Change function converttemp
to test whether the value of cels
is greater than 9. If it is greater, then add It is warm.
to msg
, otherwise add It is cold.
- you'll need the concatenation operator (..
) and a construction like msg = msg .. " some text"
. Save it and check.
12. Test your function in your sandbox with different values of the parameter to make sure that the test works as expected. Pick tests that show both messages and what happens when the temperature is 9. Consider what might happen if the parameter supplied is blank (|celsius=
) or invalid (e.g. |celsius=twenty
). Save your user sandbox with the extra tests.