User:RexxS/GCI-2018-Task03

Lua Task 3 - Calculations and tests
Prerequisite: Lua Task 2 - Working with modules. This task requires some independent learning and is more difficult.

Calculations
In 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. In the call, we will pass a parameter named  and store it inside the function as a variable named. We will then convert it to degrees Fahrenheit and store it in a variable named. Finally, we will return both values along with some text.

Use the line cel = frame.args.celsius to get the parameter  and store it in the variable.

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 fah = 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

5. Write a new line that calls your module sandbox function "temperature", 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
Note 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:  (true for numbers) is different from   (false for strings).

Lua uses the  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  as the default if no parameter is supplied. Save it.

8. Write a line in your sandbox to test that. Save it.

9. Make changes to the function  to store the text that you are returning in a variable called. Then  as the last line before the   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  to explicitly convert the variable   to a number. You'll need the  function built in to Lua. Save it and check.

11. Change function  to test whether the value of   is greater than 9. If it is greater, then add  to , otherwise add   - you'll need the concatenation operator  and a construction like. 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 or invalid (e.g. text).