Wikimedia

[Lua task #05] Perform calculations in Lua

To work on this, you must have successfully finished the task "[Lua task #04] Pass information to your Lua module" before!

This task requires some independent learning.

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 The Reference Manual if you are uncertain about anything.

1 In your module sandbox, create a new function called "temperature". In the call, we will pass a parameter named "celsius" and store it inside the function as a variable named "cel". We will then convert it to fahrenheit and store it in a variable named "fah". Finally, we will return both values along with some text.

Use the line

cel = frame.args.celsius

to get the parameter "celsius" and store it in the variable "cel".

2 To convert, we multiply a value in 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. Save your module sandbox. You'll need the concatenation (..) operator.

4 In your sandbox, write a line that calls your module sandbox function "temperature", passing 5 as the parameter, Write another line that calls the function with 25 as the parameter. Save your sandbox. Fix any errors that occur.

Task tags

  • lua
  • scribunto

Students who completed this task

Gammaburst_3000, Liza Amini, Chen Xinpeng, Yajn Bollapragada, Sathvik Hebbar, Sanath, Kid116, OHM OMAR, Tarun Prasad, suufi, Priyanshu Ahuja, Linda Thelen, Jan Nowosielski, Gabriel CH Lee, C. F. Fairfeld, Gabe Mitnick, Om Desai

Task type

  • code Code
close

2017