function start() {
var arrNums = [18,23,20,17,21,18,22,19,18,20];
var lowValue, highValue, index, count;
lowValue = Number(document.getElementById("lowValue").value);
highValue = Number(document.getElementById("highValue").value);
index = 0;
count = 0;
document.getElementById("msg").innerHTML+="The values in the array are: ";
while(index < arrNums.length) {
document.getElementById("msg").innerHTML+= arrNums[index] + " ";
index++;
}
index = 0;
if(validateLowAndHigh(lowValue, highValue) == "true") {
while(index < arrNums.length) {
if(arrNums[index] >= lowValue && arrNums[index] <= highValue) {
count++;
}
index++;
}
document.getElementById("msg").innerHTML+= "<br/> There are " + count + " values that exist in this range";
}
}
function validateLowAndHigh(low, high) {
if(low <= high) {
return("true");
} else {
document.getElementById("msg").innerHTML+= "<br/> Low value must be less than or equal to the high vaules";
return("false");
}
}
function clearOutput() {
document.getElementById("msg").innerHTML=" ";
}
有人告诉我,我不允许将true / false用作字符串,仅作为布尔值。我该如何解决这个问题?我不确定这是如何工作的,谢谢你的帮助。
答案 0 :(得分:1)
不使用return("true")
和return("false")
,而是使用return true
和return false
另外,将if(validateLowAndHigh(lowValue, highValue) == "true")
替换为
if(validateLowAndHigh(lowValue, highValue))
OR
if(validateLowAndHigh(lowValue, highValue) == true)
答案 1 :(得分:1)
像这样更改您的代码
- 更改
return true
而不是return ("true") or ("false")
- 并更改
醇>if (validateLowAndHigh(lowValue, highValue))
而不是if (validateLowAndHigh(lowValue, highValue) == 'true')
function start() {
var arrNums = [18, 23, 20, 17, 21, 18, 22, 19, 18, 20];
var lowValue, highValue, index, count;
lowValue = Number(document.getElementById("lowValue").value);
highValue = Number(document.getElementById("highValue").value);
index = 0;
count = 0;
document.getElementById("msg").innerHTML += "The values in the array are: ";
while (index < arrNums.length) {
document.getElementById("msg").innerHTML += arrNums[index] + " ";
index++;
}
index = 0;
if (validateLowAndHigh(lowValue, highValue)) {
while (index < arrNums.length) {
if (arrNums[index] >= lowValue && arrNums[index] <= highValue) {
count++;
}
index++;
}
document.getElementById("msg").innerHTML += "<br/> There are " + count + " values that exist in this range";
}
}
function validateLowAndHigh(low, high) {
if (low <= high) {
return true;
} else {
document.getElementById("msg").innerHTML += "<br/> Low value must be less than or equal to the high vaules";
return false;
}
}
function clearOutput() {
document.getElementById("msg").innerHTML = " ";
}