JavaScript · Syntax · Beginner
Functions
Basic exercises on functions in JavaScript: function declarations, parameters, return statements, number and string checks, working with arrays and simple calculations.
Quick topic start and explanations before exercises (exercises below):
function, parameters, and return
#Three function patterns
#Exercises:
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;
}
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));
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"));
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;
}
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));
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"));
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));
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;
}
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]));
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));