JavaScript · Syntax · Beginner

Functions Level 3

15 tasks

JavaScript Functions Level 3 Exercises: Functions with modes, flags, multiple conditions, string, array, and number handling to develop algorithmic thinking.

Parameters that control behaviour

#
These exercises introduce a pattern that appears constantly in real code: a parameter that controls what the function does, not what it operates on. The two most common forms are a mode string and a boolean flag. A mode string is typically one of a small set of literal values like "add", "sub", "sum", "product". The function branches on it: ```javascript function calculate(a, b, operation) { if (operation === "add") { return a + b; } else if (operation === "sub") { return a - b; } } ``` A boolean flag switches between two behaviours: ```javascript function formatText(text, upper) { if (upper) { return text.toUpperCase(); } else { return text.toLowerCase(); } } ``` The key insight: both the data (text, numbers) and the behaviour (what to do with it) are inputs to the function. This makes functions flexible without requiring a separate function for every combination. A common mistake is comparing the flag as a string: if (upper === true) is more fragile than if (upper), and if (upper === "true") will always be false when upper is an actual boolean. Boolean parameters are already true or false — use them directly.

Mode and flag patterns

#
Most exercises here combine a mode or flag with a short computation. The if/else if block is the whole function: ```javascript function scaleNumber(n, mode) { if (mode === "double") { return n * 2; } else if (mode === "triple") { return n * 3; } } function combineNumbers(numbers, mode) { if (mode === "sum") { let total = 0; for (const n of numbers) total += n; return total; } else if (mode === "product") { let result = 1; for (const n of numbers) result *= n; return result; } } ``` For array operations, the mode selects which computation to run — but both branches use the same iteration structure. Boolean flags with two symmetric behaviours: ```javascript function pickNumber(a, b, getMax) { if (getMax) { return a >= b ? a : b; } else { return a <= b ? a : b; } } ``` condition ? a : b is JavaScript's ternary operator. It is equivalent to an if/else block but fits in a single expression. Use it when both branches are short. When one branch just returns the input unchanged: ```javascript function maybeAbs(n, useAbs) { if (useAbs) { return Math.abs(n); } return n; } ``` No else needed when one path ends with return — the bare return at the end handles the other case.
01

Mode double or triple.

#

Write a function that takes a number and a mode string: "double" or "triple". If the mode is "double" — return the number multiplied by 2, if "triple" — multiplied by 3.

function scaleNumber(n, mode) {
    // your code here
}
Solution
function scaleNumber(n, mode) {
    if (mode === "double") {
        return n * 2;
    } else if (mode === "triple") {
        return n * 3;
    } else {
        return n;
    }
}
02

Uppercase flag.

#

Write a function that takes a string and a boolean value. If the upper flag is true — return the string in uppercase, otherwise — in lowercase.

function formatText(text, upper) {
    // your code here
}
Solution
function formatText(text, upper) {
    if (upper) {
        return text.toUpperCase();
    } else {
        return text.toLowerCase();
    }
}
03

Operation add or sub.

#

Write a function that takes two numbers and an operation string: "add" or "sub". Return the sum or the difference depending on the operation.

function calculate(a, b, operation) {
    // your code here
}
Solution
function calculate(a, b, operation) {
    if (operation === "add") {
        return a + b;
    } else if (operation === "sub") {
        return a - b;
    } else {
        return 0;
    }
}
04

Reverse by condition.

#

Write a function that takes a string and a number. If the number is greater than 5 — return the string reversed, otherwise — return the string unchanged.

function conditionalReverse(text, limit) {
    // your code here
}
Solution
function conditionalReverse(text, limit) {
    if (limit > 5) {
        return text.split('').reverse().join('');
    } else {
        return text;
    }
}
05

Absolute value by flag.

#

Write a function that takes a number and a flag. If the flag is true — return the absolute value, if false — return the number as is.

function maybeAbs(n, useAbs) {
    // your code here
}
Solution
function maybeAbs(n, useAbs) {
    if ((useAbs) && (n < 0)) {
        return -n;
    }
    return n;
}
06

Mode first or last.

#

Write a function that takes a string and a mode: "first" or "last". Return either the first character of the string or the last one.

function pickChar(text, mode) {
    // your code here
}
Solution
function pickChar(text, mode) {
    if (text === "") {
        return "";
    }

    if (mode === "first") {
        return text[0];
    } else if (mode === "last") {
        return text[text.length - 1];
    } else {
        return "";
    }
}
07

Sum or product.

#

Write a function that takes an array of numbers and a mode: "sum" or "product". Return the sum or the product of all elements.

function combineNumbers(numbers, mode) {
    // your code here
}
Solution
function combineNumbers(numbers, mode) {
    if (mode === "sum") {
        let total = 0;

        for (let n of numbers) {
            total += n;
        }

        return total;

    } else if (mode === "product") {
        let result = 1;

        for (let n of numbers) {
            result *= n;
        }

        return result;

    } else {
        return 0;
    }
}
08

Removing spaces by flag.

#

Write a function that takes a string and a flag. If the flag is true — remove all spaces, if false — return the string unchanged.

function cleanSpaces(text, remove) {
    // your code here
}
Solution
function cleanSpaces(text, remove) {
    if (!(remove)) {
        return text;
    }
    let result = "";
    for (let ch of text) {
        if (ch !== " ") {
            result += ch;
        }
    }
    return result;
}
09

Mode even or odd.

#

Write a function that takes a number and a mode: "even" or "odd". Return true if the number matches the mode.

function checkParity(n, mode) {
    // your code here
}
Solution
function checkParity(n, mode) {
    if (mode === "even") {
        return n % 2 === 0;

    } else if (mode === "odd") {
        return n % 2 !== 0;

    } else {
        return false;
    }
}
10

First or last 3 characters.

#

Write a function that takes a string and a mode: "short" or "long". If the mode is "short" — return the first 3 characters, if "long" — the last 3 characters.

function sliceText(text, mode) {
    // your code here
}
Solution
function sliceText(text, mode) {
    if (text.length < 3) {
        return text;
    }
    if (mode === "short") {
        return text.slice(0, 3);
    } else if (mode === "long") {
        return text.slice(-3);
    }
}
11

Greater or smaller by flag.

#

Write a function that takes two numbers and a flag. If the flag is true — return the greater number, if false — the smaller one.

function pickNumber(a, b, get_max) {
    // your code here
}
Solution
function pickNumber(a, b, getMax) {
    if (getMax) {
        return (a > b ? a : b);
    } else {
        return (a < b ? a : b);
    }
}
12

Count or length.

#

Write a function that takes a string and a mode: "count" or "length". If the mode is "count" — return the number of letters "a", if "length" — the length of the string.

function analyzeText(text, mode) {
    // your code here
}
Solution
function analyzeText(text, mode) {
    if (mode === "length") {
        return text.length;
    } else if (mode === "count") {
        let cnt = 0;
        for (let ch of text.toLowerCase()) {
            if (ch === "a") {
                cnt += 1;
            }
        }
        return cnt;
    }
}
13

Square or cube.

#

Write a function that takes a number and a flag. If the flag is true — return the square of the number, if false — the cube of the number.

function powerByFlag(n, square) {
    // your code here
}
Solution
function powerByFlag(n, square) {
    if (square) {
        return n * n;
    } else {
        return n * n * n;
    }
}
14

Trim start or end.

#

Write a function that takes a string and a mode: "start" or "end". If "start" — return the string without the first 2 characters, if "end" — without the last 2.

function trimText(text, mode) {
    // your code here
}
Solution
function trimText(text, mode) {
    if (text.length <= 2) {
        return "";
    }
    if (mode === "start") {
        return text.slice(2);
    } else if (mode === "end") {
        return text.slice(0, -2);
    }
}
15

Sign or abs.

#

Write a function that takes a number and a mode: "sign" or "abs". If the mode is "sign" — return the string "positive", "negative" or "zero", if "abs" — return the absolute value of the number.

function numberInfo(n, mode) {
    // your code here
}
Solution
function numberInfo(n, mode) {
    if (mode === "abs") {
        return (n < 0 ? -n : n);

    } else if (mode === "sign") {
        if (n > 0) {
            return "positive";

        } else if (n < 0) {
            return "negative";

        } else {
            return "zero";
        }

    } else {
        return null;
    }
}