Question: Code I have so far. To do this task I use repl.it. // Too short var password = pass; // Contains a space // password

Code I have so far. To do this task I use repl.it.

// 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; }

When creating an account online, most websites require the user to enter a "strong" password. A strong password is a password that someone is unlikely to guess. Strong passwords must be sufficiently long, contain upper and lowercase letters, contain punctuation, etc.

Complete the testPassword() function, which tests the strength of the given password. testPassword() 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 indexOf() 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 - Use substr() 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 is currently testing the password "pass" which should fail because the password is only 4 characters long. Verify that testPassword() works by trying passwords that fail each of the four criteria.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!