javascript - True/false 作为字符串,仅允许 bool 值

标签 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 作为字符串,只能使用 bool 值。我该如何解决这个问题?我不确定这是如何工作的,谢谢您的帮助。

最佳答案

像这样更改您的代码

  1. Change return true instead of return ("true") or ("false")
  2. And change if (validateLowAndHigh(lowValue, highValue)) instead of 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 = " ";
}

关于javascript - True/false 作为字符串,仅允许 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44020002/

相关文章:

php - Jquery AJAX 成功但未给出响应

javascript - 无法读取 Angular 2 中的路由参数

php - 将表单元素分布在不同的网页中,看起来简单但无法修复

javascript - 如何将非空值移动到 Javascript 数组的前面

javascript - 需要有关如何在 Express 中实际使用 Passport 身份验证策略的帮助

javascript - JQuery when() 出现错误 404

javascript - jQuery点击事件不触发

javascript - 闭包中的 var functionName 与 functionName 赋值

javascript - 如何仅启用一列值

javascript - 使用 fetch POST 方法将文件发送到服务器 API