真/假为字符串,只允许煮沸

时间:2017-05-17 08:53:14

标签: javascript

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用作字符串,仅作为布尔值。我该如何解决这个问题?我不确定这是如何工作的,谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

不使用return("true")return("false"),而是使用return truereturn false

另外,将if(validateLowAndHigh(lowValue, highValue) == "true")替换为

if(validateLowAndHigh(lowValue, highValue))

OR

if(validateLowAndHigh(lowValue, highValue) == true)

答案 1 :(得分:1)

像这样更改您的代码

  
      
  1. 更改return true而不是return ("true") or ("false")
  2.   
  3. 并更改if (validateLowAndHigh(lowValue, highValue))而不是if (validateLowAndHigh(lowValue, highValue) == 'true')
  4.   
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 = " ";
}