Reputation: 638
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>
Upvotes: 0
Views: 83
Reputation: 342
You can alter switch statement to make it work. Pass 'true' in switch and remove parentheses from case condition
switch (true) {
case num > 36:
Upvotes: 0
Reputation: 2138
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
Upvotes: 1
Reputation:
This isn't a good use case for switch
. Use if
and else
statements.
The case
s in a switch
statement are specific possible values of the expression that you're switch
ing 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.
Upvotes: 2
Reputation: 2753
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
Upvotes: 1