[英]How to use switch in JavaScript
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.
该代码应该返回带数字的16个字符,分别包含大写和小写。 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>
This isn't a good use case for switch
. 这不是
switch
好用例。 Use if
and else
statements. 使用
if
和else
语句。
The case
s in a switch
statement are specific possible values of the expression that you're switch
ing on. 该
case
在A S switch
语句是你表达的特定可能值switch
荷兰国际集团上。 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. case
语句不能是值的范围。 Using an expression as a case
is not recommended, either -- it isn't invalid , but it will probably not do what you expect either. 也不建议将表达式用作
case
,因为它不是无效的 ,但也可能不会达到您的期望。
You can't use case statements like if else so just use if else condition that will work like as following example. 您不能像if else那样使用case语句,而只能使用if else条件,该条件将像以下示例一样工作。
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 我不知道您的算法是什么,所以我只是将您的算法转换为
In the switch
block, you have written conditional statements in the case
. 在
switch
块中,您编写了case
条件语句。
You can't use conditional statements in the switch
block. 您不能在
switch
块中使用条件语句。 Hence in your case none of the case
are executing. 因此,在您的情况下,所有
case
都没有执行。 As you don't have a default
block, your function doesn't returns anything, which in case of javascript is undefined
. 由于您没有
default
块,因此您的函数不会返回任何内容(如果使用javascript,则该函数undefined
。 Hence you are getting undefined
因此,您变得
undefined
You can alter switch statement to make it work. 您可以更改switch语句以使其起作用。 Pass 'true' in switch and remove parentheses from case condition
在开关中传递“ true”并从大小写条件中删除括号
switch (true) {
case num > 36:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.