JavaScript · Syntax · Beginner
Functions Level 2
Advanced JavaScript function practice: string and number handling, logical tests, arrays, filtering, counting, value searching, and writing reusable functions.
Quick topic start and explanations before exercises (exercises below):
Patterns at this level
#Exercises:
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";
}
}
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;
}
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;
}
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;
}
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);
}
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;
}
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;
}
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;
}
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";
}
}
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;
}
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;
}
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];
}
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";
}
}
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;
}
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);
}
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;
}
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;
}
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);
}
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";
}
}
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("!", "");
}