Giter VIP home page Giter VIP logo

password-generator's Introduction

Password Generator

Technology Used

Technology Used Resource URL
javascript JavaScript https://developer.mozilla.org/en-US/docs/Web/JavaScript
html HTML https://developer.mozilla.org/en-US/docs/Web/HTML
css CSS https://developer.mozilla.org/en-US/docs/Web/CSS
git Git https://git-scm.com/

Description

Visit the Deployed Site

The purpose of this website is to supply users with a randomized password based on user selected criteria (like password length and what character types to include). Using JavaScript, the webpage will display a variety of prompts to the user about which rules their password needs to follow. After the user has given valid inputs, a series of logic is performed to generate the randomized password.

Functionality

screen-gif

Code Snippets

My initial approach to generating a random password based on user feedback consisted of two simple steps:

  1. Create one large array of all possible characters determined by the user
  2. Use a for loop (set to the length of desired password) and math.random to select random characters from the above set array

However, this purely random approach doesn't guarantee that all selected character types will be present in the final generated password. I needed to add additional logic to ensure that all required character types were included.

Step 1: Keep track of a list of indexes for each character type

    var specialIndexList= [];
    var numberIndexList = [];
    var upperIndexList = [];
    var lowerIndexList = [];
    // Declare variable for random index
    var randomIndex = 0
    // Loop through password length
    for (var i = 0; i < passwordLength; i++) {
      // Generate random index based on length of possible characters
      // Use math.floor to round down to nearest whole number
      randomIndex = Math.floor(Math.random() * possibleCharacters.length);
      // Add character at random index to password characters
      passwordCharacters.push(possibleCharacters[randomIndex]);
      // Check if character is lowercase, uppercase, number, or special character
      // add to count variable and add index to index list
      if (lowercaseLetters.includes(possibleCharacters[randomIndex])) {
        lowerIndexList.push(i);
      }
      else if (uppercaseLetters.includes(possibleCharacters[randomIndex])) {
        upperIndexList.push(i);
      }
      else if (numbers.includes(possibleCharacters[randomIndex])) {
        numberIndexList.push(i);
      }
      else if (specialCharacters.includes(possibleCharacters[randomIndex])) {
        specialIndexList.push(i);
      }

    } 

Step 2: store character list information in an object

    // create object to store character list information
    var charLists = {
      lower: {
        indexList: lowerIndexList,
        indexCount: lowerIndexList.length
      },
      upper: {
        indexList: upperIndexList,
        indexCount: upperIndexList.length
      },
      number: {
        indexList: numberIndexList,
        indexCount: numberIndexList.length
      }, 
      special: {
        indexList: specialIndexList,
        indexCount: specialIndexList.length
      },
    }

Step 3: if the specific character type is not included in the final password, choose an character index list that has at least 2 instances and replace one of the indexes with the in-question character type

    // check if user selected special characters and if password contains special characters - add if not
    if (includeSpecial && passwordCharacters.every(r=> !specialCharacters.includes(r))) {
      console.log("adding special")
      var randomSpecialIndex = Math.floor(Math.random() * specialCharacters.length);
      var addSpecial = specialCharacters[randomSpecialIndex];
      // choose an indexList that has more than two list items
      for (i=0; i<Object.keys(charLists).length; i++) {
        if (charLists[Object.keys(charLists)[i]].indexCount > 1) {
          var selectedList = Object.keys(charLists)[i];
          // stops the for loop if a list is found
          break;
        }
      }
      // replace the chosen additional character to add with a valid character selection from the password
      var chosenIndex = charLists[selectedList].indexList[charLists[selectedList].indexList.length - 1];
      console.log(selectedList)
      console.log(chosenIndex)
      console.log(charLists[selectedList])
      passwordCharacters.splice(chosenIndex, 1, addSpecial);
      // update the selectedList values since we modified the password characters
      charLists[selectedList].indexCount -= 1;
      charLists[selectedList].indexList.pop();
    }

Learning Points

  • It's easy to assume that your application works as expected when only testing certain scenarios - try to come up with possible edge cases and protect against them!
  • There are a variety of different ways to perform the desired outcome, but there is usally a more efficient method than your first attempt
  • Giving proper time and effort to reading the documentation on new technologies is crucial to project progress and quality

password-generator's People

Contributors

mccoydidericksen avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.