Odd number, really?

the output says odd.

local function OddOrEven(Number)
	if typeof(Number) ~= "number" then
		warn("Must be a number!")
	else
		local DivededResult = Number / 2
		if tostring(DivededResult):find(".") then
			return "Odd" 
		else
			return "Even"
		end
	end
	
end


local hm = OddOrEven(4)

print(hm)



1 Like

I hope this helps solve your issue.

Change to this:

local function OddOrEven(Number)
	if typeof(Number) ~= "number" then
		warn("Must be a number!")
	else
		local DivededResult = Number % 2
		if not DivededResult == 0 then
			return "Odd" 
		else
			return "Even"
		end
	end
end

local hm = OddOrEven(4)
print(hm)
local function OddOrEven(Number)
if Number % 2 == 0 then return "Even" end
return "Odd"
end

i know this topic is solved but… why did not my script work?

Passing in 4 would lead to 4/2, which results in 2.0, not 2. It finds the decimal and then says it’s odd.

1 Like

I don’t think that is the problem. That is only talking about the precision of decimal numbers not integers. Plus 4/2, 4 and 2 are powers of 2 and will not have a precision error.
@HenryThe_Legendary The real reason it happens is because of the operator /. If you divide an integer, it will return a double.

Edit: Actually, I’m not sure about the real reason it says odd, but maybe it is because of string formatting? Apparently using “.” (dot) to ‘find’ just returns 1, which is not equal to nil.

1 Like

Try this on a script:

print(string.find("abc123","."))

Apparently, using “.” as the ‘find’ just returns 1, which is not nil

I believe that the dot is a special character, meaning any character, so it matches the first character
number , number string.find ( string s, string pattern, number init = 1, bool plain = false )
maybe try this:

print(string.find("abc123",".", nil, true))

to turn plain search on, and treat the string as a raw one without regex and special stuff

> print(string.find("abc123","."))
  1 1
  > print(string.find("abc123",".", nil, true))
  nil

Try this maybe, as your code will always return Odd if the string have at least one character

local function OddOrEven(Number)
	if typeof(Number) ~= "number" then
		warn("Must be a number!")
	else
		local DivededResult = Number / 2
		if tostring(DivededResult):find(".", nil, true) then
			return "Odd" 
		else
			return "Even"
		end
	end
	
end

the fastest way to check if a number is even is this

local even = n%2 == 0

you can search what modulus (%) is if you dont know

Note that a number can only be determined if it is even or not if the number is an integer.

1 Like

I already know all that sylabus of mathematical functions, but the OP wants to know why his code doesn’t work. he’s not asking for a alternative

We are already going off topic here, but look at who I replied to.

I replied you the alternative way.
I edited a reply and made a new reply (both replying to his question) saying the reason