Wikimedia

[Lua task #07] Repeating code

To work on this, you must have successfully finished the task "[Lua task #06] Make some tests"!

This task requires independent learning and is more difficult.

Lua has several "control structures": if, while, repeat, for - this task will focus on the simple numerical for loop.

1 Examine https://en.wikipedia.org/wiki/Module:Sandbox/RexxS/GCI07 The line

local num = tonumber(frame.args.num) or 2

uses a built-in Lua function called '"tonumber" which explicitly tries to convert its argument to a number. If it fails, then 2 is used instead as the value for "num". Read through the function line-by-line and work out what each line does. The is a html tag that creates a line-break, i.e. the display continues on the next line.

2 Examine https://en.wikipedia.org/wiki/User:RexxS/sandboxGCI#Lua_Task_7 This contains "test cases" for the function, which test how it behaves when given different possible inputs: nothing, blank, text, positive number, negative number.

3 Copy the function "times" into your module sandbox. Save it.

4 Edit "times" in your module sandbox so that the heading reads something like "2 times table". Save it. Test it in your sandbox. (Do that for each following instruction).

5 Edit "times" so that it displays "2 times 1 equals 2", etc.

6 Edit "times" so that it displays each table up to 12 times instead of 10 times.

7 Leave a comment here linking to your sandbox.

Task tags

  • lua
  • scribunto

Students who completed this task

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

Task type

  • code Code
close

2017