Skip to content

Commit

Permalink
Merge pull request #14 from AllanYuen/feature/javascript
Browse files Browse the repository at this point in the history
Code for password generator and further updates to the Readme file
  • Loading branch information
AllanYuen authored Mar 20, 2024
2 parents 54a41f8 + 2af48d3 commit dfe8a25
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 11 deletions.
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ If your README is long, add a table of contents to make it easy for users to fin
- [User Story](<#user story>)
- [Acceptance Criteria](<#acceptance criteria>)
- [License](#license)
- [Features](#features)

## Installation

What are the steps required to install your project? Provide a step-by-step description of how to get the development environment running.
No special installation instructions required.

## Usage

Expand Down Expand Up @@ -60,8 +59,4 @@ THEN the password is either displayed in an alert or written to the page

This project is licensed under the [MIT License](https://opensource.org/licenses/MIT).

Please refer to the [LICENSE](./LICENSE) file for more details.

## Features

If your project has a lot of features, list them here.
Please refer to the [LICENSE](./LICENSE) file for more details.
72 changes: 68 additions & 4 deletions assets/scripts/script.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,80 @@
// Assignment code here

// Initialize required global variables for the handling of required password criteria
var pwdCriteria = {
lowerCase: "abcdefghijklmnopqrstuvwxyz",
upperCase: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
numbers: "0123456789",
specialChars: "!@#$%^&*()_+~`|}{[]\:;?><,./-=",
pwdMinLength: 8,
pwdMaxLength: 128
pwdMinLen: 8,
pwdMaxLen: 128
};

console.log(pwdCriteria);
// Defining generatePassword function as referenced by the writePassword function from the starter code
function generatePassword() {

// 1) Prompt user for secured password criteria
// - Password length to be entered between 8 to 128 characters
var pwdLength = prompt("Enter the required length of your new password (8-128 characters):");

// Validate the user input for password length
if (pwdLength < pwdCriteria.pwdMinLen || pwdLength > pwdCriteria.pwdMaxLen) {
alert("Invalid password length. Please enter a value between 8 and 128 characters.");
console.log('Selected Password Length Invalid to the Range of 8 to 128 Characters');
return;
} else {
alert(`You have chosen a password length of ${pwdLength} characters.`);
console.log(`Selected Password Length Set To: ${pwdLength}`);
}

// - Confirm password criteria for the inclusion of character types: lowercase, uppercase, numeric, and/or special characters
var lCase = confirm("Do you want to include lowercase characters?");
console.log(`Lower Case Character Criteria: ${lCase}`);
var uCase = confirm("Do you want to include uppercase characters?");
console.log(`Upper Case Character Criteria: ${uCase}`);
var num = confirm("Do you want to include numbers?");
console.log(`Numeric Character Criteria: ${num}`);
var sChar = confirm("Do you want to include special characters?");
console.log(`Special Character Criteria: ${sChar}`);

// 2) Validate criteria input to ensure at least one character type is selected
if (!lCase && !uCase && !num && !sChar) {
alert("At least one character type must be selected.");
return;
}

// Create the character set based on the user selected criteria
var charSet = "";
if (lCase) {
charSet += pwdCriteria.lowerCase;
}
if (uCase) {
charSet += pwdCriteria.upperCase;
}
if (num) {
charSet += pwdCriteria.numbers;
}
if (sChar) {
charSet += pwdCriteria.specialChars;
}
console.log(charSet);

// Randomize the character set to ensure probability of character selection not limited within the sequential construct of char ranges of above selected criteria
var newCharSet = "";
for (var i = 0; i < charSet.length; i++) {
newCharSet += charSet.charAt(Math.floor(Math.random() * charSet.length));
}
console.log(newCharSet);

// 3) Generate new password based on the user selected criteria
var newPassword = "";
for (var i = 0; i < pwdLength; i++) {
newPassword += newCharSet.charAt(Math.floor(Math.random() * newCharSet.length));
}

// 4) Display generated password within an alert or on the page
return newPassword;
};

// Get references to the #generate element
// Upon the trigger of the GENERATE PASSWORD button user input prompts to gather password criteria
Expand All @@ -22,7 +87,6 @@ function writePassword() {
var passwordText = document.querySelector("#password");

passwordText.value = password;

}

// Add event listener to generate button
Expand Down

0 comments on commit dfe8a25

Please sign in to comment.