为什么不'当用户输入空格或字符太多/不够时,此函数会发出window.alert
Why doesn't this function give window.alert when user enters spaces or too many/not enough characthers?
看起来是一个很容易解决的问题。无法弄清楚为什么代码会在输入空格或字符太多/太少的情况下返回密码。需要一点帮助。谢谢!
function secretWord() {
var text = "You entered";
var output = "Thank you, Po's secret word was validated";
<!--variable to hold secret word-->
var user_prompt = prompt("Enter Allen's secret word. It must contain exactly 7 characters and there can be no empty spaces", "");
do {
if(user_prompt.length == 6 && user_prompt.indexOf('') >= 0) {
document.getElementById("guess").innerHTML= text + "<br>" + user_prompt + "<br>" + output;
}
else if(user.prompt.length < 6) {
window.alert("secret word is too short");
var user_prompt;
document.getElementById("guess").innerHTML= text + "<br>" + user_prompt + "<br>" + output;
}
else if(user.prompt.length > 6) {
window.alert("secret word is too long")
var user_prompt;
document.getElementById("guess").innerHTML= text + "<br>" + user_prompt + "<br>" + output;
}
else if(user_prompt.indexOf('') >= 0) {
window.alert("secret word cannot contain spaces");
var user_prompt;
document.getElementById("guess").innerHTML= text + "<br>" + user_prompt + "<br>" + output;
}
}
while(user_prompt != -999);
}
除了打字错误(user_prompt != user.prompt
)之外,您正在寻找一个没有空格的7个字符的字符串。
此条件检查的内容:
if(user_prompt.length == 6 && user_prompt.indexOf('') >= 0) {
是一个包含ANY字符的6字符字符串。
你需要的是:
if(user_prompt.length == 7 && user_prompt.indexOf(' ') == -1) {
如果字符串长度为7并且没有空格,则情况就是这样。
这是一个工作示例,我对它进行了一些简化,因此在这里的代码段中更容易使用,但您可以看到并重用条件:
function secretWord() {
var text = "You entered";
var output = "Thank you, Po's secret word was validated";
var user_prompt = prompt("Enter Allen's secret word. It must contain exactly 7 characters and there can be no empty spaces", "");
document.getElementById("guess").innerHTML = '';
if (user_prompt.length == 7 && user_prompt.indexOf(' ') == -1) {
document.getElementById("guess").innerHTML = text + "<br>" + user_prompt + "<br>" + output;
} else if (user_prompt.length < 7) {
document.getElementById("guess").innerHTML = "secret word is too short";
} else if (user_prompt.length > 7) {
document.getElementById("guess").innerHTML = "secret word is too long";
} else if (user_prompt.indexOf(' ') >= 0) {
document.getElementById("guess").innerHTML = "secret word cannot contain spaces";
}
}
<div id="guess"></div>
<button onclick="secretWord()">Run</button>
else if(user.prompt.length < 6) {
// ...
else if(user.prompt.length > 6) {
在这两种情况下,user.prompt.length
都应该是user_prompt.length
您的问题在else-if语句中
它应该是user_prompt
而不是user.prompt
此外,即使您输入了空格,但字符数量正好合适,比如6个左右,它也会通过第一个if测试。您不是在检查空间,即' '
而是''
。确保你正确检查空间。
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 将函数的上下文应用于javascript变量
- 如何在JavaScript中将字符串转换为函数引用
- 用嵌套函数和默认函数定义函数
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- 无法导出函数expressjs/requestjs中的变量
- 函数参数中的数据与指定变量之间的任何性能差异
- JQuery合并了keyup和focusout两个函数
- ES6构造函数返回基类的实例
- 监视函数从服务返回不起作用,但作用域函数起作用
- 我可以在json对象中添加一个函数吗
- AngularJS:我可以跳过函数参数回调吗
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- JavaScript数组排序(函数)用于对表行进行排序,而不是排序
- jquery点击函数select&取消选择
- 拨打'父亲'函数形式a'儿童'ReactJS中的组件
- Node.js v6.2.0类扩展不是函数错误
- 比较从函数和生成的日期对象
- 为什么不'当用户输入空格或字符太多/不够时,此函数会发出window.alert
- 为什么 JavaScript 函数需要有关键字“async”?“等待”关键字还不够吗?