This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
AndreWalia's profile picture

Why is this function not working?

Started by AndreWalia, 17 December 2012 - 09:50 AM
AndreWalia #1
Posted 17 December 2012 - 10:50 AM

i = 1
fName = ""
fTable = ""
function needAName()
  if fName == fTable[i] then
	return true
  else
	return false
  end
end
function is(fName,fTable)
  for i = 1,#fTable do
	if needAName() and fName == fTable[i] then
	  ball = true
	else
	  ball = false
	end
  end
  if ball == true then
	return true
  else
	return false
  end
end
word = {
  "left",
  "right",
  "up",
  "down",
  "back",
  "front"
}
One = io.read()
if is(One,word) then
  print("correct!")
else
  print(One.." is not a side.")
end
It's not returning an error, The problem is that when i type in left it says left is not a side.
its supposed to check if fName is equal to any value in fTable and if it is then it will return true. But that's not the case I guess.
theoriginalbit #2
Posted 17 December 2012 - 10:56 AM
I think your problem is with the need nameAName() i think it should be something like this


function needAName( index )
  return fName == fTable[ index ]
end

ok can I suggest this:



function isSide(side)
  tSides = rs.getSide() -- this is a call the the redstone api which returns an array of the sides

  for i = 1, #tSides do
	if if not needAName( i ) and tSide[i] == side then
	  return true
  end

  return false
end

one = string.lower( read() )

if isSide(one) then
  print("Correct!)
else
  print(one.." is not a side")
end

remiX #3
Posted 17 December 2012 - 11:11 AM
Not sure about this but the problem might lie with the fact that you use i as a variable and in a for loop.

But TheOriginalBit's code should work just fine.