Recap - Number Generator
Part of the Logic & Flow section of Coddy's Lua journey — lesson 53 of 54.
Challenge
EasyWrite a function createNumberGenerator that takes count and returns a string showing all numbers generated by a coroutine.
Create a coroutine that generates numbers sequentially starting from 1. Each time the coroutine is resumed, it should yield the next number in the sequence. Resume the coroutine count times to collect all generated numbers, then return them as a single string with each number on a new line.
Logic:
- Create a function that uses a loop to yield numbers 1, 2, 3, etc.
- Wrap this function in a coroutine using
coroutine.create() - Resume the coroutine
counttimes, collecting each yielded number - Concatenate all numbers with newline characters between them
- Return the concatenated string
Parameters:
count(number): The number of values to generate from the sequence
Returns: A string containing all generated numbers separated by newlines (string). Format: 1\n2\n3
Try it yourself
function createNumberGenerator(count)
-- Write code here
end
All lessons in Logic & Flow
1Advanced Table Iteration
Iterating with pairs()Iterating with ipairs()pairs() vs. ipairs()Recap - Character Sheet2More Table Library Functions
table.concat()table construction & unpack()table.sort()Custom Sorting with FunctionsRecap - High Score Board3Advanced Function Concepts
Returning Multiple ValuesVariadic Functions (...)Functions First-Class ValuesAnonymous FunctionsWhat is a Closure?Recap - Simple Event Handler9Coroutines for Beginners
What is a Coroutine?coroutine create & resumePausing with coroutine.yield()resume & yieldChecking Coroutine StatusRecap - Number GeneratorRecap - Vector Math