Menu
Coddy logo textTech

Recap - Number Generator

Part of the Logic & Flow section of Coddy's Lua journey — lesson 53 of 54.

challenge icon

Challenge

Easy

Write 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 count times, 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