我一直在做这个作业,将伪代码转换为 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>