JavaScript · Syntax · Beginner

Functions

10 tasks

Basic exercises on functions in JavaScript: function declarations, parameters, return statements, number and string checks, working with arrays and simple calculations.

What functions are and why they exist

#
A function is a named block of code you can call by name, from anywhere, as many times as you need. The point is not to save typing — it is to give behaviour a name, and to make code testable and reusable. Without functions, if you need to check whether a number is even in three different places, you copy the same if (n % 2 === 0) check three times. When the logic changes, you fix it in three places. With a function, you fix it once. A function also creates a clear contract: here is what goes in (parameters), here is what comes out (return value). You can test that contract in isolation. In these exercises every task has the same format: write a function that does one specific thing. This forces you to think about inputs and outputs separately from the rest of your code — which is the core skill.

function, parameters, and return

#
JavaScript has two main ways to define a function. The classic declaration: ```javascript function square(n) { return n * n; } const result = square(5); console.log(result); // 25 ``` And the arrow function, which is common in modern code: ```javascript const square = (n) => { return n * n; }; // When the body is a single expression, the braces and return can be omitted: const square = (n) => n * n; ``` Both forms work in these exercises. Function declarations are hoisted — you can call them before the line where they are defined. Arrow functions assigned to const are not hoisted. A function without a return statement returns undefined (JavaScript's equivalent of Python's None). This is rarely what you want — always check that you are actually returning a value. There is no placeholder like Python's pass. If you need an empty function body while working, use empty braces: ```javascript function square(n) { // implement this } ``` Parameters are local to the function. Changing a parameter does not affect variables outside. Return is the correct way to communicate a result back to the caller.

Three function patterns

#
Most exercises in this topic fall into one of three patterns. Compute and return — apply an operation and send back the result: ```javascript function power(base, exp) { return base ** exp; } console.log(power(2, 10)); // 1024 ``` ** is the exponentiation operator, available since ES2016. Math.pow(base, exp) is the older equivalent. Check and return a boolean — test a condition, return true or false: ```javascript function isEven(n) { return n % 2 === 0; } console.log(isEven(8)); // true console.log(isEven(7)); // false ``` n % 2 === 0 already evaluates to true or false, so you return it directly — no if needed. Iterate and build — loop over an array inside the function, accumulate a result, return it: ```javascript function multiply(numbers) { let result = 1; for (const n of numbers) { result *= n; } return result; } console.log(multiply([3, 4, 100, 15])); // 18000 ``` String operations often combine a built-in method with return: ```javascript function toUpper(text) { return text.toUpperCase(); } ``` Short but useful — functions do not have to be long to be worth writing.
01

Square of a number with a function.

#

Write a function that takes one number and returns its square.

function square(n) {
    // your code here
}
let result = square(5);
console.log(result);
result = square(2);
console.log(result);
result = square(25);
console.log(result);
Solution
function square(n) {
    return n * n;
}
let result = square(5);
console.log(result);
result = square(2);
console.log(result);
result = square(25);
console.log(result);

// or like this using the power operator
function square(n) {
    return n ** 2;
}
02

Larger number with a function.

#

Write a function that takes two numbers and returns the larger one. If they are equal, return the second one.

function maxOfTwo(a, b) {
    // your code here
}
console.log(maxOfTwo(10, 7));
Solution
function maxOfTwo(a, b) {
    if (a > b) {
        return a;
    } else {
        return b;
    }
}
console.log(maxOfTwo(10, 7));
03

String length without len.

#

Write a function that takes a string and returns its length (do not use the .length property).

function stringLength(text) {
    // your code here
}
console.log(stringLength("javascript"));
Solution
function stringLength(text) {
    let count = 0;
    for (let _ of text) {
        count += 1;
    }
    return count;
}
console.log(stringLength("javascript"));
04

Evenness with a function.

#

Write a function that takes a number and returns true if the number is even, otherwise false.

function isEven(n) {
    // your code here
}
console.log(isEven(8));
Solution
function isEven(n) {
    if (n % 2 === 0) {
        return true;
    } else {
        return false;
    }
}
console.log(isEven(8));

// or like this using a variable
function isEven(n) {
    let result = n % 2 === 0;
    return result;
}
05

Sum of two numbers with a function.

#

Write a function that takes two numbers and returns their sum.

function add(a, b) {
    // your code here
}
console.log(add(3, 4));
Solution
function add(a, b) {
    return a + b;
}
console.log(add(3, 4));
06

String in uppercase.

#

Write a function that takes a string and returns a new string with characters in uppercase.

function toUpper(text) {
    // your code here
}
console.log(toUpper("hello"));
Solution
function toUpper(text) {
    return text.toUpperCase();
}
console.log(toUpper("hello"));
07

Positive number with a function.

#

Write a function that takes a number and returns true if it is positive, otherwise false.

function isPositive(n) {
    // your code here
}
console.log(isPositive(-3));
Solution
function isPositive(n) {
    if (n > 0) {
        return true;
    } else {
        return false;
    }
}
console.log(isPositive(-3));
08

Exponentiation.

#

Write a function that takes two numbers: base and exponent — and returns the result of raising the base to the power.

function power(base, exp) {
    // your code here
}
console.log(power(2, 3));
Solution
function power(base, exp) {
    let result = 1;
    for (let _ = 0; _ < exp; _++) {
        result *= base;
    }
    return result;
}
console.log(power(2, 3));

// or like this using the ** operator
function power(a, b) {
    return a ** b;
}
09

Product of numbers in an array.

#

Write a function that takes an array of numbers and returns their product.

function multiply(sequence) {
    // your code here
}
console.log(multiply([3, 4, 100, 15]));
Solution
function multiply(sequence) {
    let result = 1;
    for (let n of sequence) {
        result *= n;
    }
    return result;
}
console.log(multiply([3, 4, 100, 15]));
10

Repeat string with a function.

#

Write a function that takes a string and a number, and returns this string repeated the specified number of times.

function repeatText(text, count) {
    // your code here
}
console.log(repeatText(")", 3));
Solution
function repeatText(text, count) {
    let result = "";
    for (let _ = 0; _ < count; _++) {
        result += text;
    }
    return result;
}
console.log(repeatText(")", 3));