ใครที่เก่งเขียนโค๊ดอยากให้มาลองแก้โจทย์หน่อย เกี่ยวกับโค๊ดเกมส์ มีทั้งหมด 22 ข้อ

กระทู้คำถาม
ใครที่เก่งเขียนโค๊ด ลองมาแก้แต่ล่ะข้อให้ดูหน่อยครับอยากจะดูว่า ใครแก้ได้บ้าง

ข้อ 1

function factorial(n)
    if (n == 0) then
        return 1
    else
        return n * factorial(n - 1) -- The developer did not handle negative numbers. Prevent infinite recursion.
You must check if n is less than 0 (first part of if statement) and return nil.
Then use elseif for n == 0 and then else
    end
end

-----
ข้อ 2

function isEven(num)
    if (num % 2 == 1) then
        return true -- The developer wrote the condition incorrectly. Fix the logic to return true for even numbers.
    else
        return false
    end
end


----
ข้อ 3

function findMax(a, b)
    if (a > b) then
        return b -- The developer mistakenly returns the smaller value. Fix the contents of the if statement.
    else
        return a
    end
end

----
ข้อ 4

function calculateAverage(numbers)
    local sum = 0
    for i = 1, #numbers do
        sum = sum + numbers
    end
    return sum / 0 -- Division by zero error, fix it.
end
A developer tried to calculate the average of a list, but there's a division by zero error.
Hint: The divisor should be the length of the table (#numbers).

---
ข้อ 5

function findMaxValue(list)
    local max = 0
    for i, value in pairs(list) do
        if (value > max) then
            max = value
    end
    return max
end
A developer wrote a function to find the maximum value in a list, but there's a syntax error.
Hint: Ensure proper closing of if-statements.

---
ข้อ 6

function isPlayerHealthy(player)
    local health = getElementHealth(player)
    if (health > 50) then
        return true
    elseif (health < 50) then
        return false
end
end
-- This if statement fails if health is exactly 50. Remove code so that it will work correctly.

------
ข้อ 7

function printGrid(grid)
    for i, row in ipairs(grid) do
        for j, col in ipairs(row) do
            outputChatBox(col)
    end
end
A developer attempted to print a 2D grid, but there's a syntax error.
Hint: Ensure proper closing of nested loops.


-----
ข้อ 8

function sumTableValues(tbl)
    local sum = 0
    for i, v in ipairs(tbl) do
        sum = sum + i -- Error here
    end
    return sum
end
A developer wrote a function to sum the values of a table but accidentally summed the indices.


----
ข้อ 9

function readFile(filename)
    local file = fileOpen(filename)
    if (file) then
        local content = fileRead(file, fileGetSize(file))
        fileClose(file)
        return content
    end
    return -- Missing return value for error case.
end
A developer wrote a function to read a file but forgot to return something if the file is missing.
Hint: Return `false` along with an error message when the file doesn't exist, which is "File not found"

---

ข้อ 10
function giveWeaponToPlayerWithMessage(player, weaponID, ammo)
    if (isElement(player) and getElementType(player) == "player") then
        giveWeapon(player, weaponID, ammo, false) -- Forgot to notify the player.
outputChatBox(You have received a " .. getWeaponNameFromID(weaponID) .. " with  .. ammo .. " ammo.", player, 0, 255, 0)
    end
end
A developer gave a player a weapon but forgot to notify them.
Hint: He notified them (but missed 2 characters from the line) he doesn't know why the message isn't showing up, fix that


-----
ข้อ 11

function toggleSomeControls(player, state)
    if (isElement(player)) then --  Edit this to add a second part to ensure player is alive before toggling. Use "not"
        local controls = {"fire", "jump", "sprint", "crouch", "aim_weapon"}
        for i, control in ipairs(controls) do
            toggleControl(player, control, state)
        end
    end
end
A developer disabled player controls but didn't check if they were alive.
Hint: Use `isPedDead` to prevent toggling controls on dead players.

----
ข้อ 12

function activateNitroBoost(vehicle)
    if (isElement(vehicle) and getElementType(vehicle) == "vehicle") then  -- Forgot to check if the upgrade is already applied.
        addVehicleUpgrade(vehicle, 1010)
    end
end
A developer tried adding a nitro boost to a vehicle but didn't check if it already has one.
Hint: Use not and getVehicleUpgradeOnSlot (slot 8) to check if nitro is already installed.
If the nitro isn't installed already, otherwise, install it.

----
ข้อ 13

function dropPlayerWeapon(player)
    if (isElement(player) and getElementType(player) == "player") then
        local weapon = getPedWeapon(player)
        takeWeapon(player, weapon) -- Forgot to check if the player actually has a weapon.
    end
end
A developer attempted to make a player drop their weapon but didn't check if they have one.
Hint: Ensure the player has a valid weapon ID before calling `takeWeapon`.
You gotta check if weapon exists and weapon is different to 0 (~=)

----
ข้อ 14

function toggleVehicleLock(vehicle, state)
    if (isElement(vehicle) and getElementType(vehicle) == "vehicle") then
        setElementData(vehicle, "locked", state) -- Incorrect function, doesn't actually lock the vehicle.
    end
end
A developer tried to lock a vehicle but used an incorrect method.
Hint: Use `setVehicleLocked` instead.


----
ข้อ 15

function ejectPlayerFromVehicle(player)
    if (isElement(player)) then -- Missing check
        removePedFromVehicle() -- Missing argument.
    end
end
A developer attempted to eject a player from a vehicle but forgot the argument.
Hint: Use `removePedFromVehicle(player)` and check if the player is in a vehicle in the if statement.

----
ข้อ 16

function attachTrailerToVehicle(vehicle, trailer)
    if (isElement(vehicle) and isElement(trailer)) then
        attachElements(trailer, vehicle, 0, -2, 0) -- Forgot to check if the vehicle already has a trailer.
    end
end
A developer tried attaching a trailer but didn't check if the vehicle already has one.
Hint: Add if (not getVehicleTowedByVehicle(vehicle)) then

----
ข้อ 17

function setPlayerSpectateMode(player, target)
    if (isElement(player) and isElement(target)) then
        setCameraTarget(target) -- Forgot to set the camera for the correct player.
    end
end
A developer tried to make a player spectate another but set the wrong camera target.
Hint: player, target.


-----
ข้อ 18

function savePlayerData(player)
    if (isElement(player)) then
        local account = getPlayerAccount(player)
        if (account) then
            setAccountData(account, "money", getPlayerMoney()) -- Forgot to specify the player when getting money.
        end
    end
end
A developer tried to save a player's money but used `getPlayerMoney()` incorrectly.
Hint: `getPlayerMoney()` requires the player as an argument.

----
ข้อ 19

function setSafeZone(player, state)
    if (isElement(player)) then
        if (state) then
            setElementData(player, "safeZone", true)
        else
            removeElementData("safeZone") -- Forgot to specify the player when removing data.
        end
    end
end
A developer tried to toggle a player's safe zone status but incorrectly removed the element data.
Hint: Use `removeElementData(player, "safeZone")`.

----
ข้อ 20

function preventVehicleFlip(vehicle)
    if (isElement(vehicle)) then
        local rx, ry, rz = getElementRotation(vehicle)
        if (math.abs(rx) > 80 or math.abs(ry) > 80) then
            setElementRotation(0, 0, rz) -- Forgot to specify the vehicle argument.
        end
    end
end
A developer tried to prevent vehicles from flipping but made an argument mistake.

---
ข้อ 21

function kickAFKPlayers()
    for i, player in ipairs(getElementsByType("player")) do
        local lastMove = getElementData("lastMoveTime") -- Forgot to specify the player.
        if (lastMove and getTickCount() - lastMove > 300000) then -- Make it only kick logged in players using isGuestAccount and getPlayerAccount inside it.
            kickPlayer(player, "AFK too long")
        end
    end
end
A developer tried to kick AFK players but made multiple mistakes.
Hint: Ensure the player is logged in
Check if the account isn't a guest one, and getPlayerAccount(argument).

----
ข้อ 22
function preventDamageHack(attacker, weapon, bodypart)
    if (isElement(attacker) and getElementType(attacker) == "player") then
        if (weapon == 0) then
            cancelEvent -- Missing function call parentheses.
        end
    end
end
A developer attempted to prevent unarmed damage hacks but forgot to properly cancel the event.
แสดงความคิดเห็น
โปรดศึกษาและยอมรับนโยบายข้อมูลส่วนบุคคลก่อนเริ่มใช้งาน อ่านเพิ่มเติมได้ที่นี่