Javascript password setting application?

Hello. Tell me, there is an application, but I could not do it. I would appreciate if we have a member who can explain how to do it.

Easy-to-use password entered in a password field
medium - showing when passing a difficult password
APPLICATION.

Rules:
3 points if there is a uppercase letter
1 point if there is a lowercase letter
1 point if numerical value
3 points if special character
According to Total Points: 0-2 will be printed as Easy, 2-5 Medium, 5> Difficult.

Didnt we have something similar in a post recently…? Why do I feel like this is an assignment somewhere…
Anyway.

Is there a [fill in the blank] in this word would be the domain of Regular Expressions (Regex).

You’ll need String.prototype.match(), a basic understanding of Regex patterns, 5 if’s (1 elseif and an else), and some basic maths to add the numbers up.
RegexOne - Learn Regular Expressions looks like a decent basic source of learning patterns - you’ll want at least through Lesson 5 in that set if you do decide to use it.

Instead of if/else you could loop through an array and use a regex test

const passwordTypes = [
  /* [regex, points] */
  [/[A-Z]/, 3], /* Uppercase */
  [/[a-z]/, 1], /* Lowercase */
  ... same for digit
  ... same for special character
]

const password = 'A-b*&3'
let score = 0

for ([regex, points] of passwordTypes) {
  if (...your regex test here...) {
    score += points
  }
}

console.log(score)

I mean, you’re still using 4 if’s, you’ve just written it once :stuck_out_tongue:
And the if/elseif/else is for evaluating the final score.

I wasn’t criticising :slight_smile: Probably should have written ‘or you could…’. Personally I prefer working with collections over numerous if elses.

And the if/elseif/else is for evaluating the final score.

The loop achieves the same thing, no? Maybe I am missing something.

1 Like

Hehe all good, i’m just poking fun :wink:

1 Like

This looks like a job that I might get ChatGPT involved with.

Input:

Javascript program that grades a password based on a range of criteria. 3 points when the password includes an uppercase letter, 1 point when there is a lowercase letter, 1 point when there is a numeric value, and 3 points when there is a special character. It should show a grade of Easy when the total some of those criteria is 2 or less, Medium if the grade is 5 or less, and Difficult when the grade is more than that.

Output:

Sure, here’s an example JavaScript program that grades a password based on the criteria you mentioned:

function gradePassword(password) {
  let score = 0;
  if (/[A-Z]/.test(password)) { // Check for uppercase letter
    score += 3;
  }
  if (/[a-z]/.test(password)) { // Check for lowercase letter
    score += 1;
  }
  if (/\d/.test(password)) { // Check for numeric value
    score += 1;
  }
  if (/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test(password)) { // Check for special character
    score += 3;
  }
  
  if (score <= 2) {
    return "Easy";
  } else if (score <= 5) {
    return "Medium";
  } else {
    return "Difficult";
  }
}

// Example usage
console.log(gradePassword("password123")); // Output: Easy
console.log(gradePassword("Pa$w0rd")); // Output: Difficult

The gradePassword function takes a password as input and calculates its score based on the criteria you mentioned. It uses regular expressions to check if the password contains an uppercase letter, a lowercase letter, a numeric value, and a special character. It adds up the points for each criteria and returns a grade based on the total score.

You can test the function by calling it with different passwords and checking the output. In the example usage, we call the function with two passwords and print the output to the console. The first password has a score of 2, so it’s graded as Easy. The second password has a score of 7, so it’s graded as Difficult.


It gets better though. I then asked for the code to be improved.


Input:

rewrite using modern techniques and best practices

Output:

Certainly! Here’s a modernized version of the previous code using best practices:

function gradePassword(password) {
  const criteria = [
    /[A-Z]/, // Uppercase letter
    /[a-z]/, // Lowercase letter
    /\d/,    // Numeric value
    /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/ // Special character
  ];
  
  const score = criteria.reduce((acc, regex) => {
    return acc + regex.test(password);
  }, 0);
  
  if (score <= 2) {
    return "Easy";
  } else if (score <= 5) {
    return "Medium";
  } else {
    return "Difficult";
  }
}

// Example usage
console.log(gradePassword("password123")); // Output: Easy
console.log(gradePassword("Pa$w0rd")); // Output: Difficult

The new implementation uses an array of regular expressions to represent the password criteria. This makes it easier to add or modify criteria in the future. We also use the reduce function to calculate the score based on the criteria. The reduce function iterates over each regular expression and adds 1 to the accumulator if the password matches the criteria. Finally, we use an if/else statement to return the password grade based on the score.

We also use const and let instead of var to declare variables, and we use arrow functions instead of traditional function expressions. These are both modern JavaScript features that help make the code more readable and maintainable.

Overall, the modernized version of the code is more concise, readable, and easier to maintain than the original implementation.


It lost the scoring criteria of the password, but more importantly I want to achieve the outcome of using no if statements at all in the code :slight_smile:

Input:

Rewrite that updated code but without using if statements this time.

Output:

Certainly, here’s the updated version of the code that doesn’t use if statements:

function gradePassword(password) {
  const criteria = [
    /[A-Z]/, // Uppercase letter
    /[a-z]/, // Lowercase letter
    /\d/,    // Numeric value
    /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/ // Special character
  ];
  
  const score = criteria.reduce((acc, regex) => acc + regex.test(password), 0);
  
  return score <= 2 ? "Easy" : score <= 5 ? "Medium" : "Difficult";
}

// Example usage
console.log(gradePassword("password123")); // Output: Easy
console.log(gradePassword("Pa$w0rd")); // Output: Difficult

The updated code uses a ternary operator to return the password grade instead of an if/else statement. The ternary operator is a concise way to write conditional statements that return a value.

In this case, the first condition checks if the score is less than or equal to 2. If it’s true, it returns “Easy”. If it’s false, it checks the second condition, which checks if the score is less than or equal to 5. If it’s true, it returns “Medium”. Otherwise, it returns “Difficult”.

Overall, this version of the code is even more concise and easier to read than the previous one.


The scores have got lost in that update, so I complain to ChatGPT to fix that.


Input:

The code needs to score 3 points when the password includes an uppercase letter, 1 point when there is a lowercase letter, 1 point when there is a numeric value, and 3 points when there is a special character.

Output:

I apologize for the confusion earlier. Here’s the updated code that scores the password correctly according to the given criteria:

function gradePassword(password) {
  const criteria = [
    { regex: /[A-Z]/, score: 3 }, // Uppercase letter
    { regex: /[a-z]/, score: 1 }, // Lowercase letter
    { regex: /\d/, score: 1 },    // Numeric value
    { regex: /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/, score: 3 } // Special character
  ];
  
  const score = criteria.reduce((acc, { regex, score }) => {
    return acc + (regex.test(password) ? score : 0);
  }, 0);
  
  return score <= 2 ? "Easy" : score <= 5 ? "Medium" : "Difficult";
}

// Example usage
console.log(gradePassword("password123")); // Output: Easy
console.log(gradePassword("Pa$w0rd")); // Output: Difficult

In this updated version, we define the criteria as an array of objects, where each object contains a regular expression and a score. We use destructuring to extract the regex and score properties from each object in the reduce function. Then, we calculate the score by adding the corresponding score to the accumulator if the password matches the regular expression, instead of adding 1 to the accumulator for each criteria that matches the password.

Now the code should correctly grade the passwords based on the range of criteria specified.


Thank you ChatGPT. Some things got lost along the way, such as the custom scoring, but by understanding waht the code should do, it’s relatively easy to get things back on track again.

This of course is not an application, but it is a good core part of the full thing that might want to be used.

And finally, regardless of whether this is a homework assignment or not, some good learnings are to be found here.

1 Like

I was missing something. I see Paul is typing…

@Paul_Wilkins

const score = criteria.reduce((acc, regex) => {
    return acc + regex.test(password);
  }, 0);

This is adding acc + (true or false) // 1 or 0. Is that the intention?

It’s essentially the same as the for of loop I did.

for ([regex, points] of passwordTypes) {
  if (regex.test(password)) { // filled in the condition
    score += points
  }
}

Yeah… should… probably be… acc + regex.text(password) * score ? (yes, i’m abusing implicit type coercion … bool * int = int; true coerces to 1)

It was a work in progress. Complaining about the quality of the code helped to fix that problem, where the relevant scores are suitably added up.

My greatest concern there is that ChatGPT thinks it can get away with shoddy work. You’ve really got pile the pressure on to get any decent code from it. :slight_smile:

When you get down to it though, the CPU is just chock-a-block full of if statements. There’s no getting away from it. You can only hide it behind higher and higher walls of abstraction.

4 Likes