JavaScript · Syntax · Beginner

Functions Level 2

20 tasks

Advanced JavaScript function practice: string and number handling, logical tests, arrays, filtering, counting, value searching, and writing reusable functions.

Thinking through a function

#
At this level the functions are still small, but the logic requires combining several tools: loops, string methods, conditions, and return values. The challenge is deciding which tools to use. Before writing the body, ask three questions: what type is the input, what type should come out, and what operation connects them? For counting something in a string — vowels, letters, words — the pattern is: loop over the string, check each character, maintain a counter, return it. ```javascript function countVowels(text) { const vowels = "aeiouy"; let count = 0; for (const char of text.toLowerCase()) { if (vowels.includes(char)) { count++; } } return count; } ``` text.toLowerCase() handles case before checking. vowels.includes(char) tests whether the character is in the vowel string. For string transformations — reverse, remove characters, trim edges — JavaScript string methods often give you a clean one-liner: ```javascript function reverseText(text) { return text.split("").reverse().join(""); } function removeSpaces(text) { return text.split(" ").join(""); } function trimEdges(text) { return text.slice(1, -1); } ``` split("").reverse().join("") is the idiomatic way to reverse a string in JavaScript: split into individual characters, reverse the array, join back. slice(1, -1) removes the first and last character — negative index in slice() counts from the end. For numeric properties — last digit, absolute difference, divisibility: ```javascript function lastDigit(n) { return Math.abs(n) % 10; } function absDiff(a, b) { return Math.abs(a - b); } function divisibleBy3And5(n) { return n % 3 === 0 && n % 5 === 0; } ``` Math.abs() is the JavaScript equivalent of Python's abs(). It handles negative inputs so the last digit of -47 is still 7, not -7.

Patterns at this level

#
A palindrome check compares the string to its reverse: ```javascript function isPalindrome(text) { return text === text.split("").reverse().join(""); } ``` Case sensitivity matters. "Racecar" reversed is "racecaR" — not equal. For production you would also strip spaces and convert to lowercase first. The exercises here test simple cases. Counting words with split: ```javascript function countWords(text) { return text.trim().split(/\s+/).length; } ``` split(/\s+/) splits on one or more whitespace characters and handles extra spaces. Without trim(), a leading space would create an empty first element. Filtering and summing from an array: ```javascript function sumPositive(numbers) { let total = 0; for (const n of numbers) { if (n > 0) { total += n; } } return total; } ``` Removing specific characters — iterate and rebuild: ```javascript function removeExclamations(text) { let result = ""; for (const char of text) { if (char !== "!") { result += char; } } return result; } ``` Alternatively with the replace method and a regex: ```javascript function removeExclamations(text) { return text.split("!").join(""); } ``` Both forms are valid. The loop is more readable for beginners; the split/join version is more idiomatic JavaScript.
01

Even or Odd.

#

Write a function that takes a number and returns the string "Even" if the number is even, and "Odd" if it is odd.

function evenOrOdd(n) {
    // your code here
}
Solution
function evenOrOdd(n) {
    if (n % 2 === 0) {
        return "Even";
    } else {
        return "Odd";
    }
}
02

Reverse a string with a function.

#

Write a function that takes a string and returns it in reversed form.

function reverseText(text) {
    // your code here
}
Solution
function reverseText(text) {
    return text.split('').reverse().join('');
}

// or like this using a loop
function reverseText(text) {
    let result = "";
    for (let char of text) {
        result = char + result;
    }
    return result;
}
03

Arithmetic mean.

#

Write a function that takes two numbers and returns their arithmetic mean.

function average(a, b) {
    // your code here
}
Solution
function average(a, b) {
    return (a + b) / 2;
}
04

Count vowels.

#

Write a function that takes a string and returns the number of vowels in it. Vowels: a, e, i, o, u, y (case does not matter).

function countVowels(text) {
    // your code here
}
Solution
function countVowels(text) {
    let vowels = "aeiouy";
    let count = 0;

    for (let ch of text.toLowerCase()) {
        if (vowels.includes(ch)) {
            count += 1;
        }
    }

    return count;
}
05

Divisibility by 3 and 5.

#

Write a function that takes a number and returns true if it is divisible by both 3 and 5, otherwise false.

function divisibleBy3And5(n) {
    // your code here
}
Solution
function divisibleBy3And5(n) {
    return (n % 3 === 0) && (n % 5 === 0);
}
06

String without spaces.

#

Write a function that takes a string and returns it without spaces.

function removeSpaces(text) {
    // your code here
}
Solution
function removeSpaces(text) {
    let result = "";
    for (let ch of text) {
        if (ch !== " ") {
            result += ch;
        }
    }
    return result;
}
07

Sum of positive elements.

#

Write a function that takes an array of numbers and returns the sum of positive elements.

function sumPositive(numbers) {
    // your code here
}
Solution
function sumPositive(numbers) {
    let total = 0;
    for (let n of numbers) {
        if (n > 0) {
            total += n;
        }
    }
    return total;
}
08

Palindrome check.

#

Write a function that takes a string and returns true if it is a palindrome.

function isPalindrome(text) {
    // your code here
}
Solution
function isPalindrome(text) {
    return text === text.split('').reverse().join('');
}

// or like this: prepare the reversed string separately
function isPalindrome(text) {
    let reversed = text.split("").reverse().join("");
    return text === reversed;
}
09

Positive, Negative or Zero.

#

Write a function that takes a number and returns the string: "Positive", "Negative" or "Zero".

function describeNumber(n) {
    // your code here
}
Solution
function describeNumber(n) {
    if (n > 0) {
        return "Positive";
    } else if (n < 0) {
        return "Negative";
    } else {
        return "Zero";
    }
}
10

Number of words in a string.

#

Write a function that takes a string and returns the number of words in it. Words are separated by spaces.

function countWords(text) {
    // your code here
}
Solution
function countWords(text) {
    if (text === "") {
        return 0;
    }
    return text.trim() ? text.trim().split(/\s+/).length : 0;
}

// or like this: save the intermediate array of words into a variable
function countWords(text) {
    let words = text.trim() ? text.trim().split(/\s+/) : [];
    return words.length;
}
11

Multiple of ten.

#

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

function isMultipleOfTen(n) {
    // your code here
}
Solution
function isMultipleOfTen(n) {
    return n % 10 === 0;
}
12

First and last character function.

#

Write a function that takes a string and returns its first and last character as one string.

function firstAndLast(text) {
    // your code here
}
Solution
function firstAndLast(text) {
    if (text === "") {
        return "";
    }
    return text[0] + text[text.length - 1];
}
13

Yes if greater than 100.

#

Write a function that takes a number and returns the string "Yes" if the number is greater than 100, otherwise "No".

function moreThanHundred(n) {
    // your code here
}
Solution
function moreThanHundred(n) {
    if (n > 100) {
        return "Yes";
    } else {
        return "No";
    }
}
14

Counting the letter a.

#

Write a function that takes a string and returns the number of letters "a" in it (case-insensitive).

function countA(text) {
    // your code here
}
Solution
function countA(text) {
    let count = 0;
    for (let ch of text.toLowerCase()) {
        if (ch === "a") {
            count += 1;
        }
    }
    return count;
}
15

Absolute difference.

#

Write a function that takes two numbers and returns their absolute difference.

function absDiff(a, b) {
    // your code here
}
Solution
function absDiff(a, b) {
    let diff = a - b;
    if (diff < 0) {
        diff = -diff;
    }
    return diff;
}

// or like this with Math.abs
function difference(a, b) {
    return Math.abs(a - b);
}
16

Even string length.

#

Write a function that takes a string and returns true if its length is even, otherwise false.

function isEvenLength(text) {
    // your code here
}
Solution
function isEvenLength(text) {
    return text.length % 2 === 0;
}
17

Last digit of a number.

#

Write a function that takes a number and returns its last digit.

function lastDigit(n) {
    // your code here
}
Solution
function lastDigit(n) {
    if (n < 0) {
        n = -n;
    }
    return n % 10;
}
18

Without the first and last letter.

#

Write a function that takes a string and returns it without the first and last letter.

function trimEdges(text) {
    // your code here
}
Solution
function trimEdges(text) {
    if (text.length <= 2) {
        return "";
    }
    return text.slice(1, -1);
}
19

Small, Medium or Large.

#

Write a function that takes a number and returns a string: - "Small" — if the number is less than 10 - "Medium" — if it is from 10 to 100 - "Large" — if it is greater than 100

function sizeLabel(n) {
    // your code here
}
Solution
function sizeLabel(n) {
    if (n < 10) {
        return "Small";
    } else if (n <= 100) {
        return "Medium";
    } else {
        return "Large";
    }
}
20

Removing exclamation marks.

#

Write a function that takes a string and returns it without all exclamation marks "!".

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

// or like this with replaceAll
function removeExclamations(text) {
    return text.replaceAll("!", "");
}