Week 18
Source Code
local myName = ""
print("Name: " .. myName)
-- define main function
function main()
    print("Hello Main Function")
    print(myName)
    local done = false
    local choice = nil
    while not done do
        print("Menu")
        print("E1 - Example 1")
        print("Q - Quit")
        io.write("Choice: ")
        choice = io.read()
        if choice == "E1" then
            -- Example 1 Function call
            example1()
        elseif choice == "E2" then
            -- Example 2 Function call
            example2()
        elseif choice == "E3" then
            -- Example 3 Function call
            example3()
        elseif choice == "E4" then
            -- Example 4 Function call
            example4()
        elseif choice == "Q" then
            done = true
        else
            print("Invalid Choice")
        end
    end
end
-- define Example 1 Function
function example1()
    print("Example 1")
    -- define friends list
    
    -- print the number of elements
   
    -- print the element one by one
    
    -- using a for loop, print the index and elements
    
    -- assign value to first friend
    
    -- assign value to the last friend
    
    -- print the friends
    
end
-- define Example 2 Function
function example2()
    -- create an empty list
    
    -- add an element to the end of the list (append)
    
    -- add an element to the end of the list (append)
    
    -- add an element to the beginning of the list (insert)
    
    -- print using for in loop
    
    -- print using for in loop with index
    
    -- delete a fruit
    
    -- print using for in loop
    
end
-- define Example 3 Function
function example3()
    
end
-- define Example 4 Function
function example4()
    
end
-- call to main function, do not delete!
main()w18problem1.lua
-- function definition
function problem1()
end
-- function definition
function main()
    -- function call
end
-- call to main function
main()
w18problem2.lua
-- function definition
function problem2()
end
-- function definition
function main()
    -- function call
end
-- call to main function
main()
w18problem3.lua
-- function definition
function problem3()
end
-- function definition
function main()
    -- function call
end
-- call to main function
main()
w18problem4.lua
-- function definition
function problem4()
end
-- function definition
function main()
    -- function call
end
-- call to main function
main()
w18problem5.lua
-- function definition
function problem5()
end
-- function definition
function main()
    -- function call
end
-- call to main function
main()
w18problem6.lua
-- function definition
function problem6()
end
-- function definition
function main()
    -- function call
end
-- call to main function
main()
playlist.lua
-- Main function
function main()
    -- Initialize playlist with 10 songs
    local playlist = {}
    local done = false
    while not done do
        print("My Playlist Manager")
        print("1. View playlist")
        print("2. View playlist with index positions")
        print("3. Add a song")
        print("4. Remove a song")
        print("5. Select a random song")
        print("6. Sort playlist")
        print("7. Shuffle playlist")
        print("8. Reorder playlist")
        print("9. Clear playlist")
        print("10. Exit")
        io.write("Choose an option (1-10): ")
        local choice = io.read("*n")
        io.read()
        if choice == 1 then
            print("My current playlist")
        elseif choice == 2 then
            print("My current playlist with index positions")
            
            
        elseif choice == 3 then
           print("Add a song")
        elseif choice == 4 then
            print("Remove a song")
        elseif choice == 5 then
            print("Here's a random song!")
            
        elseif choice == 6 then
            print("Sort my playlist alphabetically")
            
            
        elseif choice == 7 then
            print("Shuffle playlist")
            -- Loop through the list from the last index to the second index. 
                -- Generate a random index between 1 and i
                
                -- Swap elements at index i and j
                
        elseif choice == 8 then
            print("Reorder playlist")
            
        elseif choice == 9 then
            print("Playlist cleared.")
            
        elseif choice == 10 then
            print("Goodbye!")
            done = true
        else
            print("Invalid choice. Please try again.")
        end
    end
end
-- Call the main function
main()
Last updated
Was this helpful?