I'm trying to make a calculator that can identify order of operations and I hit upon a road block. The variable "thinking" (a string) is supposed to return the product/quotient of the operation but instead it stays the same even after i modify it with gsub. I have tried many fixes over several days including several redos of the entire code but i have not been able to figure it out.
Testing mode: Play(f5)
Script type: LocalScript, inside of PlayerGui
Output: (This repeats infinitely, but that's not my issue because if the gsub worked it wouldn't happen) There is multiplication in the operation. - Client - LocalScript:39 The targeted operation is: * - Client - LocalScript:43 First number is:123 - Client - LocalScript:61 Second number is:123 - Client - LocalScript:62 End result:15129 - Client - LocalScript:68 Thinking:123*123 - Client - LocalScript:70
Expected Output: There is multiplication in the operation. - Client - LocalScript:39 The targeted operation is: * - Client - LocalScript:43 First number is:123 - Client - LocalScript:61 Second number is:123 - Client - LocalScript:62 End result:15129 - Client - LocalScript:68 Thinking:15129 - Client - LocalScript:70
Code:~~~~~~~~~~~~~~~~~ --this is where the issue is local function calculate1() local thinking = a.Text while true do local d = {} local v1 = string.find(thinking, "*") local v2 = string.find(thinking, "/") local firstnumber = nil local secondnumber = nil local endresult = nil
if v1 then table.insert(d, 1, v1) print("There is multiplication in the operation.") end if v2 then table.insert(d, 1, v2) print("There is division in the operation.") end if not v1 and not v2 then break end table.sort(d) local firstoperation = d[1] print("The targeted operation is: "..string.char(string.byte(thinking, firstoperation)))
for i = firstoperation - 1, 1, -1 do local currentchar = string.byte(thinking, i) if currentchar == b[11] or currentchar == b[12] or currentchar == b[13] or currentchar == b[14] then firstnumber = string.sub(thinking, i + 1, firstoperation - 1) elseif i == 1 then firstnumber = string.sub(thinking, i, firstoperation - 1) end end
for i = firstoperation + 1, string.len(thinking), 1 do local currentchar = string.byte(thinking, i) if currentchar == b[11] or currentchar == b[12] or currentchar == b[13] or currentchar == b[14] then secondnumber = string.sub(thinking, firstoperation + 1, i - 1) elseif i == string.len(thinking) then secondnumber = string.sub(thinking, firstoperation + 1, i) end end
print("First number is:"..firstnumber) print("Second number is:"..secondnumber) if string.byte(thinking, firstoperation) == b[13] then endresult = firstnumber * secondnumber elseif string.byte(thinking, firstoperation) == b[14] then endresult = firstnumber / secondnumber end ~~~~~~~~~~~~~~~~~
print("First number is:"..firstnumber) print("Second number is:"..secondnumber) if string.byte(thinking, firstoperation) == b[13] then endresult = firstnumber * secondnumber elseif string.byte(thinking, firstoperation) == b[14] then endresult = firstnumber / secondnumber end
Help would be appreciated :)
Edit: The page only shows 12 lines of code (the gsub is where the issue is) but the mumbo jumbo abve and below it is the rest of the code.