Coding challenges for beginners

Just want this said first. I haven’t seen a single post about this so that’s why I’m bringing this up. Anyways

Challenge 1 : Create a function that prints out the players WalkSpeed + the players Velocity

Challenge 2 : Create a function that loops trough a table and adds all the tables value together
*Note : 3 Integers inside the table,

The tables leftover arrays have to be sorted from biggest to smallest

Table example :

--//Example

local arrays = {
	10, --[1]
	25, --[2]
	19 --[3]
}

Challenge 3 : Now that you’ve added up all the tables arrays i want you to use the example table as mentioned before, then i want you to find the largest Int inside of the table to then erase it from the table.

My solutions to the 2nd and the 3d challenge :

SPOILER !

repeat wait() until game:IsLoaded() --//love this

local arrays = {
	10, --[1]
	25, --[2]
	19 --[3]
}

local i = 0 ---//declare our base value

local function addArrays()
	for _, v in ipairs(arrays) do --//for loop the tables arrays
		i = i + v --//add all the arrays together inside the table
	end
	print(i) --//print the declared variable(basically the tables values all together OUTSIDE the for loop)
end

local function removeArray()
	table.sort(arrays, function(a, b) --//declare the tables path using table.sort and the returing the greatest value to set it to : 25, 19, 10
		return a > b --//returns the tables arrays in the table.sort order 25, 19, 10
	end)
	table.remove(arrays, 1) --//remove the largest array that now is [1] or 25
	
	print(arrays) --// the table will look something like this now : [1] = 19, [2] = 10
end

addArrays() --//call the created functions
removeArray() --//call the created functions
1 Like

Alternatively (if you wish to make the third challenge a bit harder, doing it without table.sort):

local arrays = {
	10, --[1]
	25, --[2]
	19 --[3]
}

local compare = 0
local comparelocation

for i, v in pairs(arrays) do
	if compare > v then
		continue
	else
		compare = v
		comparelocation = i
	end
end

table.remove(arrays, comparelocation)

print(arrays)
2 Likes

I mean, This is for sure a solution though the table wouldn’t be sorted after you’d remove the largest array, It would look like this :

[1] = 10,
[2] = 19
1 Like

Ah, I could for sure add that, got confused since it’s not mentioned in the third challenge.

1 Like

yeah, That’s my bad i’ll make sure to add that.

1 Like