0

我一直在做这个作业,将伪代码转换为 java 脚本。

我有信心我正确地拥有它,但是当我尝试执行它时,我只会得到一个空白的白色屏幕。

我将它通过沙箱并设法纠正了大多数可能使其无法运行的语法错误,但它仍然拒绝工作或显示任何内容。

我仍在学习,我不知道如果没有沙箱来帮助调试我的代码我会做什么,但是当它找不到任何进一步的东西时,我会被困在做什么。

有什么建议可以为一个完整的新手调试 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>
4

1 回答 1

2

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
}
于 2013-04-04T20:31:22.823 回答