0

I have been trying to make a password generator and I have this code and it is returning "undefined" not a character. The code is supposed to return 16 characters with numbers, lower and upper characters. I want to have this so that I don't have to worry about making my own passwords anymore.

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>PassGen</title>
  <script>
    function getRandomInt(min, max) {
      return Math.floor(Math.random() * (max - min + 1)) + min;
    }

    function num2lett(num) {
      switch (num) {
        case (num > 36):
          return String.fromCharCode(577 + num - 35);
          break;
        case (num > 10 && num < 37):
          return String.fromCharCode(97 + num - 11);
          break;
        case (num == 10):
          return "0";
          break;
        case (num < 10):
          return "" + num;
          break;
      }

    }

    function uuidMake() {
      var uuid = "";
      for (u = 0; u < 16; u++) {
        randNum = getRandomInt(0, 62);
        uuid += num2lett(randNum);
      }
      document.getElementById("password").innerHTML = uuid;
    }
  </script>
  <style>
    @font-face {
      font-family: "uuid_font";
      src: url("Courier Prime Code.ttf") format("TrueType");
    }
    
    body {
      background-color: #262626;
    }
    
    #password {
      font-family: "uuid_font";
      text-align: center;
      font-size: 30px;
      color: #dddddd;
      margin-top: 250px;
    }
  </style>

  <body>
    <p id="password" onclick="uuidMake();">Click Me</p>
  </body>

</html>

4 Answers 4

2

This isn't a good use case for switch. Use if and else statements.

The cases in a switch statement are specific possible values of the expression that you're switching on. For instance, one might write something like:

switch (n) {
    case 1:
        // things to do when n is 1
        break;
    case 3:
    case 4:
        // things to do when n is 3 or 4
        break;
    default:
        // things to do when n is neither 1, 3, nor 4
}

A case statement cannot be a range of values. Using an expression as a case is not recommended, either -- it isn't invalid, but it will probably not do what you expect either.

Sign up to request clarification or add additional context in comments.

Comments

1

You can't use case statements like if else so just use if else condition that will work like as following example.

function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

function num2lett(num) {
  if (num > 36)
    return String.fromCharCode(577 + num - 35);
  else if (num > 10 && num < 37)
    return String.fromCharCode(97 + num - 11);
  else if (num == 10)
    return "0";
  else if (num < 10)
    return "" + num;
}

function uuidMake() {
  var uuid = "";
  for (u = 0; u < 16; u++) {
    randNum = getRandomInt(0, 62);
    uuid += num2lett(randNum);
  }
  document.getElementById("password").innerHTML = uuid;
}
@font-face {
  font-family: "uuid_font";
  src: url("Courier Prime Code.ttf") format("TrueType");
}

body {
  background-color: #262626;
}

#password {
  font-family: "uuid_font";
  text-align: center;
  font-size: 30px;
  color: #dddddd;
  margin-top: 250px;
}
<p id="password" onclick="uuidMake();">Click Me</p>

What is your algorithm I don't know so I am just converted your algorithm to if else

2 Comments

Most of this is working, I'm a bit worried about the caps letters though, any idea on how to fix that? I though it was 577 (unicode)
Replace this line first if condition by this line if (num > 36) return String.fromCharCode(65 + num - 35);
1

In the switch block, you have written conditional statements in the case.

You can't use conditional statements in the switch block. Hence in your case none of the case are executing. As you don't have a default block, your function doesn't returns anything, which in case of javascript is undefined. Hence you are getting undefined

Comments

0

You can alter switch statement to make it work. Pass 'true' in switch and remove parentheses from case condition

switch (true) {
   case num > 36: 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.