Recursive function returning empty string

Hello there, hope you are doing well.

local function recursiveSearch(prompt)
	local rand = math.random(#dic)
	local longRand = math.random(#long)
	local exRand = math.random(#expert)
	local jkRand = math.random(#jklm)
	local hyRand = math.random(#hyphenated)
	
	prompt = prompt:upper()
	
	if string.find(dic[rand], prompt) then
		print(dic[rand])
		return dic[rand]
	elseif string.find(long[longRand], prompt) then
		print(long[longRand])
		return long[longRand]
	else
		recursiveSearch(prompt)
	end
end

(I know that there is an elseif chain, I can’t think of how to make it a table switch thing.)
The code above generates a random number, and checks in a dictionary if the chosen word contains the letters given (prompt). Now, this does work well, and the print function print the word chosen (which does in fact have the letters given). But the thing is, when I try this:

print(recursiveSearch("FA"))

(just an example)
It prints an empty string. I tried everything I can think of and nothing worked.
If someone could help that would be amazing.

Thank you!

Put a breakpoint and step through the code, should reveal where it goes wrong

It’s because you’re not returning anything in the else block

You should change it to:

return recursiveSearch(prompt)

This worked but ended up freezing my game for a few seconds
EDIT: Nevermind, a print function was freezing the game

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.