我对该函数仅遇到除整数之外的问题,我需要它仅接受 2-15 个字符之间有效的字符串? 感谢您的帮助。
function getDescription() {
var Description = [];
Description = prompt("Enter the description: ", "");
while (!(Description >= 2 && Description <= 15))
{
Description = prompt("Error! The description must be between (2 - 15) characters in length.\nEnter the description: ", "");
}
return Description;
}
getDescription()
编辑:我认为我遇到的另一个问题是我输入的内容实际上是作为数组存储在描述中吗?
最佳答案
prompt
函数的返回值是一个字符串,因此您需要检查该字符串的长度,而不是它的值:
while (!(Description.length >= 2 && Description.length <= 15)){
// ...
}
关于javascript - 函数只接受整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11375992/