即使在调试之后也不会运行的简短java脚本代码

Short Java-script code that will not run, even after debugging

本文关键字:java 代码 脚本 运行 调试 之后      更新时间:2023-09-26

我一直在做这项任务,将伪代码转换为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
}