Project: password checker
JavaScript · Unit 6: Strings
- Full plan
- JavaScript
- Project
- 8 exercises
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. Predict the output
What does this code print?
2. Complete the code
Complete the function that says whether the password has at least 8 characters.
3. Predict the output
What does this code print?
4. Write the code
A safe password has at least one digit. Write
hasDigit(password), which returnstrueif some character is a digit andfalseif none of them is.5. Find the bug
hasUppershould say whether the password has a capital letter. With "hello" it should printfalse. Which line has the error?6. Multiple choice
What does this function do?
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. 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".
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.