Test your skills: Loops

This aim of this skill test is to assess whether you've understood our Looping code article.

Note: You can try out solutions for the tasks below by downloading the code, putting it in an online tool such as CodePen, jsFiddle, or Glitch, then working on the tasks. We didn't provide live editable versions of these tasks because of the risk of creating infinite loops and crashing the assessment page!

If you get stuck, then ask us for help — see the Assessment or further help section at the bottom of this page.

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 Manipulating documents tutorial.

Loops 1

In our first looping task we want you start by creating a simple loop that goes through all the items in the provided myArray and prints them out on the screen inside list items (i.e., <li> elements), which are appended to the provided list.

Download the starting point for this task to work in your own editor or in an online editor.

Loops 2

In this next task, we want you to write a simple program that, given a name, searches an array of objects containing names and phone numbers (phonebook) and, if it finds the name, outputs the name and phone number into the paragraph (para) and then exits the loop before it has run its course.

You are given three variables to begin with:

  • i — starts off with a value of 0; intended to be used as an iterator.
  • name — contains a name to search for
  • para — contains a reference to a paragraph, which will be used to report the results.

You should use a type of loop that you've not used in the previous task.

Download the starting point for this task to work in your own editor or in an online editor.

Loops 3

In this final task, you are provided with the following:

  • i — starts off with a value of 500; intended to be used as an iterator.
  • para — contains a reference to a paragraph, which will be used to report the results.
  • isPrime() — a function that, when passed a number, returns true if the number is a prime number, and false if not.

You need to use a loop to go through the numbers 2 to 500 backwards (1 is not counted as a prime number), and run the provided isPrime() function on them. For each number that isn't a prime number, continue on to the next loop iteration. For each one that is a prime number, add it to the paragraph's textContent along with some kind of separator.

You should use a type of loop that you've not used in the previous two tasks.

Download the starting point for this task to work in your own editor or in an online editor.

Assessment or further help

You can practice these examples in the Interactive Editors above.

If you would like your work assessed, or are stuck and want to ask for help:

  1. Put your work into an online shareable editor such as CodePen, jsFiddle, or Glitch. You can write the code yourself, or use the starting point files linked to in the above sections.
  2. Write a post asking for assessment and/or help at the MDN Discourse forum Learning category. Your post should include:
    • A descriptive title such as "Assessment wanted for Loops 1 skill test".
    • Details of what you have already tried, and what you would like us to do, e.g. if you are stuck and need help, or want an assessment.
    • A link to the example you want assessed or need help with, in an online shareable editor (as mentioned in step 1 above). This is a good practice to get into — it's very hard to help someone with a coding problem if you can't see their code.
    • A link to the actual task or assessment page, so we can find the question you want help with.