User:RexxS/GCI-2019-Task04

Lua Task 4 - Loops and tables
Prerequisite: Lua Task 3 - Calculations and tests. This task requires independent learning and is more difficult.

Loops
Lua has several control structures  - this task will focus on the simple numerical for loop.

1. Examine this code:

Line 2,, uses a built-in Lua function called   which explicitly tries to convert its argument to a number. If it fails, then 2 is used instead as the value for the variable numb. The variable is created as, which means that it is destroyed when the program leaves the section where it is defined. The variable numb only exists within the function  and not in the rest of the module. This is called its scope. It is a good idea to keep a variable's scope as small as is needed to avoid clashes with other variables using the same name. Variables that are not defined as local are called global and we will generally avoid using them unless absolutely necessary.

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. Add a comment  to your module sandbox before the final line.

3. Copy the function  into your module sandbox after the comment. Save it.

4. In your user sandbox create a new level 2 heading for this task at the end of the sandbox by typing

5. Add a new line that invokes the times function from your module sandbox supplying 4 as the parameter. Use the preview to make sure that you get a four-times table. Save your user sandbox.

6. Edit  in your module sandbox so that it displays a heading  like 4 times table above the table. Do it in such a way that the heading will always show the number of the times table passed as the parameter. Save it. Check it in your user sandbox by refreshing the page and fix any errors.

7. In your user sandbox add four more test cases for the function, which test how it behaves when given different possible inputs: nothing, blank, text, and a negative number. Use the preview to make sure that the first three show the default times table (2 times) and save your user sandbox. Fix any errors.

8. Edit  in your module sandbox so that it will display something like 5 times 1 equals 5, etc. Save it. Check it in your user sandbox by refreshing the page and fix any errors.

9. Edit  in your module sandbox so that it displays each table up to 12 times instead of 10 times. Save it. Check it in your user sandbox by refreshing the page and fix any errors.

Tables
A simple table is demonstrated in the following code:

10. Make a copy of the function "p.people" in your module sandbox after the previous function. Use blank lines to keep your functions separate and tidy. Save your module sandbox.

11. Make a new level 3 section at the end of your user sandbox:

and create a test for your "people" function in your user sandbox. Once it is working, save the user sandbox.

Make sure you understand why the test displays Hello Betty. The table friends is a simple sequence, i.e. it is indexed by consecutive integers. Unlike other programming languages you may have used, Lua starts its indices at 1, rather than 0.

Note that friends[n] (where n is a variable containing a number) will refer to the nth member of the friends table.

12. Amend the code in your module sandbox so that it will display Hello Agnetha instead, and save it. Check its output in your user sandbox.

13. Amend the code in your module sandbox so that it uses a numeric  loop around line 4 which loops from   to   so that the output will be five lines, the first one displaying "Hello Agnetha", the next "Hello Betty", and so on, showing the members of the friends table.

14. Amend the code in your module sandbox so you add two more names to the end of the "friends" table, and save it. Check in your user sandbox that the two new names won't automatically display, because the  loop only goes from 1 to 5.

You can find the size of a simple table like friends by using  which gives the number of members of the sequence table.

15. Amend the code in your module sandbox so that the upper limit of the  loop is. That ensures that it will always display all of the friends members even if the number of them changes. Check in your user sandbox that the two new names now display.

16. Amend the code in your module sandbox to add one more name to the end of the friends table. Check in your user sandbox that it also displays.