You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
a function that returns true if number is similar to another
What is the issue? Include screenshots / videos if possible!
I do not know how to do it
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
yes
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.
If I understand, you answer on your question. Just pick up your max number by math.max(number, number) then reduction it and you have difference. Last is just how big difference you want to achieve
function isSimilar(n1,n2)
if math.max(n1,n2) - math.min(n1,n2) <= 5 then -- Maximum Number between the Two - The Minimum Number, if it is less than or equal to 5 it returns true.
return true
else
return false
end
end
print(isSimilar(21,25)) -- Will return true
print(isSimilar(25,30)) -- Will also return true
print(isSimilar(500,1000)) -- Will return false
print(isSimilar(21,31)) -- Will also return false
Well, it’s an alternative though this is one weird question there is no context for what it’s going to be used for so it’s hard to judge the best solution.
Here is alternative which should be the same as your method which I believe is simpler though using absolute difference.
--similarity threshold is 5 units apart of each other.
function isSimilar(n1,n2,threshold)
threshold = threshold or 5
if math.abs(n1-n2) <= threshold then -- Absolute difference
return true
else
return false
end
end
As I stated, he can change the threshold, it is essentially pointless to use a percentage method as you can just change the threshold to suit your needs.