Test your skills: Functions
The aim of this skill test is to help you assess whether you've understood our Functions — reusable blocks of code, Build your own function, and Function return values articles.
Note: To get help, read our Test your skills usage guide. You can also reach out to us using one of our communication channels.
DOM manipulation: considered useful
Some of the questions below require you to write some DOM manipulation code to complete them — such as creating new HTML elements, setting their text contents to equal specific string values, and nesting them inside existing elements on the page — all via JavaScript.
We haven't explicitly taught this yet in the course, but you'll have seen some examples that make use of it, and we'd like you to do some research into what DOM APIs you need to successfully answer the questions. A good starting place is our DOM scripting introduction tutorial.
Interactive challenge
First of all, we are giving you a fun, interactive challenge involving function return values, created by our learning partner, Scrimba.
Watch the embedded scrim, and complete the task on the timeline (the little ghost icon) by following the instructions and editing the code. When you are done, you can resume watching the scrim to check how the teacher's solution matches up with yours.
Task 1
To complete our first functions task:
- Define a function—
chooseName()
—that prints a random name from the provided array (names
) into the provided paragraph (para
). - Call the
chooseName()
function once.
const names = [
"Chris",
"Li Kang",
"Anne",
"Francesca",
"Mustafa",
"Tina",
"Bert",
"Jada",
];
const para = document.querySelector("p");
// Don't edit the code above here!
// Add your code here
Click here to show the solution
Your finished JavaScript should look something like this:
// ...
// Don't edit the code above here!
function chooseName() {
const randomNumber = Math.floor(Math.random() * names.length);
const choice = names[randomNumber];
para.textContent = choice;
}
chooseName();
Task 2
This task requires you to create a function that draws a rectangle on the provided <canvas>
(reference variable canvas
, context available in ctx
), based on the five provided input variables:
x
— the x coordinate of the rectangle.y
— the y coordinate of the rectangle.width
— the width of the rectangle.height
— the height of the rectangle.color
— the color of the rectangle.
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
const x = 50;
const y = 60;
const width = 100;
const height = 75;
const color = "blue";
// Don't edit the code above here!
// Add your code here
Click here to show the solution
Your finished JavaScript should look something like this:
// ...
// Don't edit the code above here!
function drawSquare(x, y, width, height, color) {
ctx.fillStyle = "white";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = color;
ctx.fillRect(x, y, width, height);
}
drawSquare(x, y, width, height, color);
Task 3
In this task, you return to the problem posed in Task 1, with the aim of making three improvements to it.
To complete the task:
- Refactor the code that generates the random number into a separate function called
random()
, which takes as parameters two generic bounds that the random number should be between, and returns the result. - Update the
chooseName()
function so that it makes use of the random number function, takes the array to choose from as a parameter (making it more flexible), and returns the result. - Print the returned result into the paragraph (
para
)'stextContent
.
const names = [
"Chris",
"Li Kang",
"Anne",
"Francesca",
"Mustafa",
"Tina",
"Bert",
"Jada",
];
const para = document.querySelector("p");
// Don't edit the code above here!
// Update the code below here
function chooseName() {
const randomNumber = Math.floor(Math.random() * names.length);
const choice = names[randomNumber];
para.textContent = choice;
}
chooseName();
Click here to show the solution
Your finished JavaScript should look something like this:
// ...
// Don't edit the code above here!
function random(min, max) {
const num = Math.floor(Math.random() * (max - min)) + min;
return num;
}
function chooseItem(array) {
const choice = array[random(0, array.length)];
return choice;
}
para.textContent = chooseItem(names);
Task 4
In this task, we have an array of names, and we're using Array.filter()
to get an array containing only the names shorter than 5 characters. The filter is currently being passed a named function isShort()
. This checks the length of the name, returning true
if the name is less than 5 characters long, and false
otherwise.
To complete the task, update the code so that the functionality inside isShort()
is instead included directly inside the filter()
call as an arrow function. See how compact you can make it.
const names = [
"Chris",
"Li Kang",
"Anne",
"Francesca",
"Mustafa",
"Tina",
"Bert",
"Jada",
];
const para = document.querySelector("p");
// Don't edit the code above here!
// Update the code below here
function isShort(name) {
return name.length < 5;
}
const shortNames = names.filter(isShort);
para.textContent = shortNames;
Click here to show the solution
Your finished JavaScript should look something like this:
// ...
// Don't edit the code above here!
// Update the code below here
const shortNames = names.filter((name) => name.length < 5);
para.textContent = shortNames;