Javascript functions

Alright so earlier today on the r/roblox reddit I saw a cool function a developer made and in the comments he showed the site he had gotten the idea from (https://codepen.io/jkiss/pen/OVEeqK) and I wanted to recreate it, the problem is that I suck at all java and I would like to know if it was possible for someone to give me tips on how I could turn this:

function getRandomSpeed(pos) {
    var min = -1,
        max = 1;
    switch (pos) {
        case "top":
            return [randomNumFrom(min, max), randomNumFrom(0.1, max)];
            break;
        case "right":
            return [randomNumFrom(min, -0.1), randomNumFrom(min, max)];
            break;
        case "bottom":
            return [randomNumFrom(min, max), randomNumFrom(min, -0.1)];
            break;
        case "left":
            return [randomNumFrom(0.1, max), randomNumFrom(min, max)];
            break;
        default:
            return;
            break;
    }
}

as well as this:

function getRandomBall() {
    var pos = randomArrayItem(["top", "right", "bottom", "left"]);
    switch (pos) {
        case "top":
            return {
                x: randomSidePos(can_w),
                y: -R,
                vx: getRandomSpeed("top")[0],
                vy: getRandomSpeed("top")[1],
                r: R,
                alpha: 1,
                phase: randomNumFrom(0, 10)
            };
            break;
        case "right":
            return {
                x: can_w + R,
                y: randomSidePos(can_h),
                vx: getRandomSpeed("right")[0],
                vy: getRandomSpeed("right")[1],
                r: R,
                alpha: 1,
                phase: randomNumFrom(0, 10)
            };
            break;
        case "bottom":
            return {
                x: randomSidePos(can_w),
                y: can_h + R,
                vx: getRandomSpeed("bottom")[0],
                vy: getRandomSpeed("bottom")[1],
                r: R,
                alpha: 1,
                phase: randomNumFrom(0, 10)
            };
            break;
        case "left":
            return {
                x: -R,
                y: randomSidePos(can_h),
                vx: getRandomSpeed("left")[0],
                vy: getRandomSpeed("left")[1],
                r: R,
                alpha: 1,
                phase: randomNumFrom(0, 10)
            };
            break;
    }
}

into a lua functioning scripts.

Here are some tips:

  1. You don’t need the brackets, you only need an

    end

at the back.
2. you don’t need ; at every statement.

Yeah I know about those but case and switch I dont know how java script works in that sense, I know how to script in lua.

  • The switch expression is evaluated once.
  • The value of the expression is compared with the values of each case.
  • If there is a match, the associated block of code is executed.
  • If there is no match, the default code block is executed.
switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}

Check out this documentation page by w3schools.

Lua doesn’t have switch statements. Here’s what the code would look like transcribed into Lua:

function getRandomSpeed(pos)
	local min = -1, max = 1
	if (pos == "top") then
		return {math.random(min, max), math.random(0.1, max)}
	elseif (pos == "right") then
		return {math.random(min, -0.1), math.random(min, max)}
	elseif (pos == "bottom") then
		return {math.random(min, max), math.random(min, -0.1)}
	elseif (pos == "left") then
		return {math.random(0.1, max), math.random(min, max)}
	end
end

And:

local array = {"top", "right", "bottom", "left"}
function getRandomBall()
	local pos = array[math.random(#array)]
	if (pos == "top") then
		return {
			x = randomSizePos(can_w),
			y = -R,
			vx = getRandomSpeed("top")[0],
			vy = getRandomSpeed("top")[1],
			r = R,
			alpha = 1,
			phase = math.random(0, 10)
		}
	elseif (pos == "right") then
		return {
			x = can_w + R,
			y = randomSizePos(can_h),
			vx = getRandomSpeed("right")[0],
			vy = getRandomSpeed("right")[1],
			r = R,
			alpha = 1,
			phase = math.random(0, 10)
		}
	elseif (pos == "bottom") then
		return {
			x = randomSizePos(can_w),
			y = can_h + R,
			vx = getRandomSpeed("bottom")[0],
			vy = getRandomSpeed("bottom")[1],
			r = R,
			alpha = 1,
			phase = math.random(0, 10)
		}
	elseif (pos == "left") then
		return {
			x = -R,
			y = randomSidePos(can_h),
			vx = getRandomSpeed("left")[0],
			vy = getRandomSpeed("left")[1],
			r = R,
			alpha = 1,
			phase = math.random(0, 10)
		}
	end
end