user7912728
user7912728

Reputation:

True/false as strings, only allowed boolen

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=" ";
}

I was told that I am not allowed to use true/false as strings, only as boolean. How do I go about fixing this? I am not sure how this works, thank you for the help.

Upvotes: 0

Views: 58

Answers (2)

prasanth
prasanth

Reputation: 22490

Change your code as like this

  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 = " ";
}

Upvotes: 1

AamirR
AamirR

Reputation: 12198

Instead of using return("true") and return("false"), use return true and return false

Also, replace if(validateLowAndHigh(lowValue, highValue) == "true") with

if(validateLowAndHigh(lowValue, highValue))

OR

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

Upvotes: 1

Related Questions