How to make my calculator more efficient?

Hey guys I’m making a calculator. There is an issue though. It’s just so tedious to write! Is there a way to make this better? (This is for addition)

local addition = function(num1,num2)
	if num1 == 1 and num2 == 0 then
		return 1
	else if num1 == 1 and num2 == 1 then
		return 2
	else if num1 == 1 and num2 == 2 then
		return 3
	else if num1 == 1 and num2 == 3 then
		return 4
	else if num1 == 1 and num2 == 4 then
		return 5
	else if num1 == 1 and num2 == 5 then
		return 6
	else if num1 == 1 and num2 == 6 then
		return 7
	else if num1 == 1 and num2 == 7 then
		return 8
	else if num1 == 1 and num2 == 8 then
		return 9
	else if num1 == 1 and num2 == 9 then
		return 10
	else if num1 == 1 and num2 == 10 then
		return 11
	end
end

It just takes too long to write

4 Likes

I may be misunderstanding the exact behind the scenes of this, but you could just return

num1 + num2

without an if statement.

1 Like

local addition = function(num1,num2)
return num1+num2
end

you have a very interesting way of doing it

1 Like

For subtraction, you could do the same, but with the appropriate sign.

In code, these are

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /
1 Like

Just saying num1 + num2 isn’t proper syntax. Nothing happens when I try it :frowning:

I get this error: ServerScriptService.Script:2: Incomplete statement: expected assignment or a function call

image

How do we know what num1 and num2 are, wouldn’t this always return 3 because 1 + 2 = 3?

You should write “return” before, so

-- Add the two numbers provided (NOTE: the ": number" is a type annotation; it's like saying "This IS a number". It is not required)
local addition = function(num1: number, num2: number)
    return num1 + num2
end
1 Like

Yes I am well aware that it is a number. That’s what I’m adding, numbers.

Num1 and Num2 are simply the names of these arguments; what you provide to the function for it to work with.

For example:

local function addition(num1: number, num2: number)
    return num1 + num2
end

-- Num1 would be equal to 1, and num2 would be equal to 3.
addition(1, 3) -- Returns 4

4 minus 1 equals 3, hence what I was saying earlier. But I’m doing addition, not subtraction.

Yeah – sorry. I was just explaining arguments since I thought you weren’t fully aware.

How do we know what num1 and num2 are, wouldn’t this always return 3 because 1 + 2 = 3?

Sorry if I seemed rude, I didn’t mean to start an argument.
image
I’m trying to count rocks being dropped in my game.

Nothing was returned. Do you know why?

So, if you are trying to count the rocks in your game, you could do the following:

  • Put all the rocks into a folder in the workspace, we’ll call it “Rocks” for now.
  • Write an 'ol function that counts the number of rocks in that folder… so:
local Rocks = workspace.Rocks

-- Returns the amount of rocks
local function countRocks()
    return #Rocks:GetChildren() -- # is the number of something
end

-- These basically fire each time a rock is added or removed.
Rocks.DescendantAdded:Connect(countRocks)
Rocks.DescendantRemoving:Connect(countRocks)

Sorry if I misunderstood.

Oh – and don’t forget to put the rock inside that folder wherever you make your rocks :stuck_out_tongue:

But the works are in workspace (as shown in image below) not in a folder.

Ah – that’s my game! Let me explain what’s happening:

The folder is inside the workspace for the rocks. Since the folder is in the workspace, anything inside said folder will be visible in the workspace. I hope I explained that correctly.

Erm… this isn’t your game? It’s part of my rock counting game…

2 Likes

he is trolling if you haven’t realized

2 Likes

Yeah. Okay so back to scripting the calculator. Is that working well?

No. I only have three rocks for some reason.

1 Like