Question
Must be done in Javascript. When creating an account online, most websites require the user to enter a strong password. A strong password is a
Must be done in Javascript.
When creating an account online, most websites require the user to enter a "strong" password. A strong password is a password that a hacker is unlikely to guess. Strong passwords must be sufficiently long, contain upper and lowercase letters, contain punctuation, etc.
Complete the testPassword(password) function which tests the strength of the given password. The testPassword() function should verify the password meets the criteria below in the order specified. If the criteria is not met, testPassword() should return an appropriate message indicating what is wrong with the password. If all the criteria are met, testPassword() should return an empty string.
1.)Minimum length of 6 characters - Use the .length property to ensure the password is long enough.
2.)No spaces - Use the .indexOf() method to ensure the password does not contain any spaces.
3.)Use at least one digit - Create a loop to examine each character of the password, and count how many times a digit character appears. JavaScript does not have a function to verify if a character is a digit, so use the isSingleDigit() function provided. The password should have at least one digit.
4.)First 3 characters must not be repeated at the end - This requirement requires the use of .substr() or .substring() to extract the string at the front and end of the password. Then, compare the substrings with ===. Ex: Password "abc123abc" is not acceptable because "abc" at the front of the password is the same as "abc" at the end of the password.
The code below is testing the password "pass" which should fail because the password is only 4 characters long. Verify that the testPassword function works by trying passwords that fail each of the four criteria.
// Too short var password = "pass";
// Contains a space // password = "Contains space";
// Doesn't use a digit // password = "my-password";
// Repeats first and last 3 chars // password = "abc123abc";
// Strong password // password = "StrongPassword1";
// See if function returns an error message or not var message = testPassword(password); if (message) { console.log(message); } else { console.log("Password accepted."); } function testPassword(password) { // return "Password must be at least 6 characters."; // return "Password may not contain a space."; // return "Password must have at least one digit."; // return "The password may not begin and end with the same 3 characters."; // Everything is good return ""; }
// Returns true if n is a string with a single digit, false otherwise function isSingleDigit(n) { var unicodeValue = n.charCodeAt(0); return n.length === 1 && unicodeValue >= 48 && unicodeValue <= 57; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started