JavaScript · Syntax · Beginner
Functions Level 3
JavaScript Functions Level 3 Exercises: Functions with modes, flags, multiple conditions, string, array, and number handling to develop algorithmic thinking.
Quick topic start and explanations before exercises (exercises below):
Mode and flag patterns
#Exercises:
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;
}
}
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();
}
}
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;
}
}
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;
}
}
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;
}
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 "";
}
}
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;
}
}
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;
}
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;
}
}
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);
}
}
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);
}
}
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;
}
}
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;
}
}
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);
}
}
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;
}
}