Home > Software design >  Java: return back true IF AND ONLY IF all of the list numbers are greater than zero
Java: return back true IF AND ONLY IF all of the list numbers are greater than zero

Time:12-22

I am having trouble with returning back true IF AND ONLY IF all of the list numbers are greater than zero. It should return back true IF AND ONLY IF all of the list numbers are greater than zero. -5 is in the list, but after the program is run, it returns true, when it should be false ("Some of the numbers are not positive").

var allPositive = false;

var numberList = [2, -5, 1, 50, 4, 82, 34];

checkIfAllPositive();

console.log(allPositive);

if (allPositive == true)
{
  console.log("All of the numbers are positive!");
}
else
{
  console.log("Some of the numbers are not positive!");
}



function checkIfAllPositive()
{
  for (var index = 0; index < numberList.length; index  ) {
    console.log(numberList[index]   " "   index   " "  (numberList[index] > 0));
    if (numberList[index] <= 0)
    {
      allPositive = false;
    } else {
      allPositive = true;
    }
  }
}

CodePudding user response:

When you loop through your array you need to break after one of them is false.

Like so:

function checkIfAllPositive()
{
  for (var index = 0; index < numberList.length; index  ) {
    console.log(numberList[index]   " "   index   " "  (numberList[index] > 0));
    if (numberList[index] <= 0)
    {
      allPositive = false;
      break;
    } else {
      allPositive = true;
    }
  }
}

This is because once one of them is false it will set allPositive to false, but then it will keep going and if it finds another positive one then it will set allPositive to true even though there's a negative number in there.

CodePudding user response:

There are some quirks in your function that you have to tune.

To solve your problem there are different ways:

The more efficient and useful to learn is this:

    var numberList = [2, -5, 1, 50, 4, 82, 34];

    function checkIfAllPositive(list) {
        for (var index = 0; index < list.length; index  ) {
            if (list[index] <= 0) return false
        }
        return true
    }

    var allPositive = checkIfAllPositive(numberList)
    console.log(allPositive)

Visit all array elements and as soon you discover a negative (or 0) one return false. If you peruse all the array elements without returning a false, you have to return true as all values are positives.

This code obey to a general rule Avoid side effects as much as possible:

  • You will not change any outside value from the body of a function.
  • You will not read any value in a function without naming it in the parameters declaration.

They are the twelfth and the Thirteenth commandments of the Programmer Book. It is a life saver rule you need to enforce almost everywhere, specially with Javascript that has some scope caveats that can drive you mad.

You code has a critical flaw, apart relying on side effects for input and output: When you evaluate a value in your list, you will lost the evaluation of the previous one. Apart my preferred procedural solution above, if you want to stick to the rule one entry -> one exit that I will recommend only for long functions. You have to account on this, maybe counting the negative values (sol b)

function checkIfAllPositive(numberList)
{
  var negatives = 0;
  for (var index = 0; index < numberList.length; index  ) {
    console.log(numberList[index]   " "   index   " "   (numberList[index] > 0));
    if (numberList[index] <= 0)
    {
      negatives  = 1;
    } 
  }
  return negatives > 0;
}

Or taking in account for previous values

function checkIfAllPositive(numberList)
{
  var allPositiveSoFar = true;
  for (var index = 0; index < numberList.length; index  ) {
    console.log(numberList[index]   " "   index   " "   (numberList[index] > 0));
    if (numberList[index] <= 0)
    {
      // this is equivalent to allPosivitesSoFar = allPositiveSoFar && false;
      allPositivesSoFar = false;
      
    } else {
      allPosivitesSoFar = allPositivevSoFar && true;
  }
  return negatives > 0;
}

If you want to have a little of fun you can also use a bit of functional programming to solve your problem in one line using the Array filter method:

var numberList = [2, -5, 1, 50, 4, 82, 34]
var allPositive = numberList.filter(a => a <= 0).length == 0
console.log(allPositive)

You invert your test and select (ie create a new array containing them) all the values that will cause a false response. If there is at least one, the expression will be false.

It is a not so efficient way to solve your task as the function filter will check all the elements even if the first one is negative.

A better approach is to use the Array some method

var numberList = [2, -5, 1, 50, 4, 82, 34]
var allPositive = !numberList.some(a => a <= 0)
console.log(allPositive)

I think it is self explanatory: the numbers are all positive if there aren't (!) negative values

  • Related