Skip to the content

Project: password checker

JavaScript · Unit 6: Strings

To know whether a character is a digit, ask a string with the ten digits whether it includes it.

That way you need no new method.

const digits = "0123456789";
for (const c of "ab3") {
  console.log(c, digits.includes(c));
}

Prints

a false
b false
3 true

The rest of the explanation is in the lesson, which is part of the full plan.

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Predict the output

    What does this code print?

  2. 2. Complete the code

    Complete the function that says whether the password has at least 8 characters.

  3. 3. Predict the output

    What does this code print?

  4. 4. Write the code

    A safe password has at least one digit. Write hasDigit(password), which returns true if some character is a digit and false if none of them is.

  5. 5. Find the bug

    hasUpper should say whether the password has a capital letter. With "hello" it should print false. Which line has the error?

  6. 6. Multiple choice

    What does this function do?

  7. 7. Find the case that fails

    firstUpper(password) should return the first capital letter of the password, or an empty string "" if it has none. Which call breaks it?

  8. 8. Write the code

    Write check(password) using the two functions you already have. Return the first message that applies, in this order: with fewer than 8 characters, "Short: N/8", where N is its length; with no digits, "Needs a digit"; with no capitals, "Needs a capital"; if it passes everything, "Strong".

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →