我一直致力于这项任务,将伪代码转换为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>
答案 0 :(得分:2)
true和false区分大小写。
将True更改为true 和假到假
你可以像调试器一样使用firebug(对于firefox来说是addon),或者对于chrome只需按F12键来调试器。对于上述情况,你肯定会在控制台中出错。
在javascript中没有名为isNumeric()或isLetter()的函数。你可以试试这个
if(isNaN(parseInt(char1)))
{
//it is a string
}
else
{
//it is a number
}