即使在调试之后也不会运行的简短java脚本代码
Short Java-script code that will not run, even after debugging
我一直在做这项任务,将伪代码转换为java-script。
我确信我写对了,但是当我试图执行它时,我只得到一个空白的屏幕。
我把它放在沙箱中,并设法纠正了大多数可能使它无法运行的语法错误,但它仍然拒绝工作或显示任何内容。
我还在学习,我不知道如果没有沙盒来帮助调试我的代码我会怎么做,但是当它找不到任何进一步的东西时,我陷入了怎么办的困境。
有什么建议调试java脚本代码为一个完整的新手?
这是代码:
<html>
<body>
<script type="text/javascript">
// Declare variables
var username; // potential username entered by user
var char1; // one character extracted from username
var anyDigits = False; // variable to signify presence of digits
var index; // loop variable for extracting characters
var BR = "<br />";
var ES = "";
// Display program heading and requirements and ask for username
document.write("This program helps you set up a valid username." + BR);
document.write("Your username must be at least 8 characters long," + BR);
document.write(" start with a letter, and contain at least 1 digit." + BR);
username = prompt("Please enter your name: ", ES);
// Check for length of username
while (username.length < 8) {
document.write("ERROR...your username must be at least 8 characters long." + BR);
username = prompt("Please enter your new username: ", ES);
}
// Check that first character is a letter
// Substring function has three arguments: string, starting position, and ending position
char1 = username.substr(0, 0);
while (char1 !== isLetter()) {
document.write("ERROR: the first character of your username must be a letter." + BR);
username = prompt("Please enter your new username: ", ES);
}
// Check that there's at least one digit in the username
while (anyDigits !== True) {
// Check each character, set anyDigits to true if a digit
for (index = 1; index < username.substr(index, index); index++) {
char1 = username.substr(index, index);
if (isNumeric(char1)) {
anyDigits = True;
}
}
// If anyDigits is still false, no digits were present
if (anyDigits !== True) {
document.write("ERROR:...your username must include at least 1 digit." + BR);
username = prompt("Please enter your new username: ", ES);
}
}
// Thank the user and end the program
document.write("Thank you! Your new username is: " + username);
</script>
</body>
</html>
true和false区分大小写。
将True更改为True而False为False
你也可以使用调试器,如firebug (firefox插件)或chrome只需按F12键调试器。对于上述情况,您肯定会在控制台中得到一个错误。
在javascript中没有isNumeric()或isLetter()这样的函数。你可以试试这个
if(isNaN(parseInt(char1)))
{
//it is a string
}
else
{
//it is a number
}
相关文章:
- 如何轻松地将服务器端变量从Java代码转移到客户端代码
- 我需要java代码来打开html代码onclick事件
- 在Java代码中的javascript函数中使用变量
- 如何使用jsp以相同的形式调用java代码和javascript代码
- 如何从javascript运行java代码
- 如何使用按钮从 JSP 页面执行 Java 代码
- 为什么Java代码需要编译,而JavaScript代码不需要
- 从javascript调用android原生java代码
- GWT:在Java代码中捕获本地JSNI异常
- 从本机 java 代码调用 JS 事件
- 如何从本机 Java 代码调用 JS
- 适用于 Android 应用程序的 Java 代码中的实例化异常
- 如何从 java 代码 android native 调用 JavaScript 函数
- HTMLUnit是否包括一个功能性的[HTML5]画布2D实现,能够将图像数据渲染回Java代码
- 来自javascript的Java代码
- CryptoJS和Java代码的不同输出加密
- 使用 Java 代码在 HTML 文件中运行 JavaScript 代码
- 使用 java 代码获取完整的网页
- 将javascript函数的参数传递给java代码
- 使用 cordova-plugin-crosswalk-webview 从 JS 调用本机 Java 代码