Lesson 2 of 10
Numbers and strings
- 13 min
- 6 checks
- 3 hints
- Go to task
Goal
The //, % and ^ operators and string functions
Explanation
Lua 5.4 has two kinds of numbers: integer and float, and math.type(x) tells you which kind a number is. Division with / always returns a float, so 10 / 2 gives 5.0. // divides and rounds the result down, and % gives the remainder, so 250 // 4 gives 62 and 250 % 4 gives 2. That's how loot gold gets split among the members of a party.
The power operator ^ always returns a float, even when both numbers are integers, so 2 ^ 3 gives 8.0. That's why the XP formula 100 * level ^ 2 shows up in the UI as 900.0 for level 3, and math.floor turns it back into an integer. To display a float with two decimal places, use string.format("%.2f", x).
You call string functions with a colon (:), as in nick:upper(), nick:sub(1, 3), which takes the first through third characters, and ("#"):rep(5), which repeats the string. #nick gives you its length. But # and sub count bytes, not characters, and a letter like é takes two bytes, so #"José" gives 5. To count the characters in text like that, use utf8.len, and keep in mind that upper leaves é unchanged.
Your task
Divide gold by party: make share each player's cut as an integer, and leftover what's left after the division. Then set xpNext to 100 * level ^ 2 as an integer, and accuracy to the percentage of shots that were hits, as a string with two decimal places. Finally, make tag the first three letters of nick in uppercase, inside square brackets, and bar one # for every 10 hp.
Checks
6 checks- share is the integer 62, not 62.5
- leftover is the remainder, 2
- xpNext is the integer 900, not 900.0
- accuracy is the string "71.15", with two decimal places
- tag is [SHA]
- bar has seven # signs