0

The task is to build a function where it receives an array and a number that will work as a limit. The thing is that it should return an array with the resulting booleans like this:(i.e. [true, false, false]). But I can't figure out how.

I tried using a for loop to stuff an empty array, but it returns just false.

function aperturas(arrayDeIngresosSemanales, cantMinEst) {

    for (var i = 0; i < arrayDeIngresosSemanales.length; i++) {

    var a = 0;
    var arr = arrayDeIngresosSemanales[i];

    for (var j = 0; j < arr.length; j++) {



    if (arr[j] <= 0) {
        a = a + 1;
      }

    }
    if (a >= cantMinEst) {

      return true;
    } else {

      return false;
    }
  }

}

aperturas([0, 0, 3, 0], [1, 2, 4, 5], [0, 0, -1], 3);

3
  • 1
    Don't return from the inner loop. Create an array, and instead of returning .push() the boolean value onto it. Then return that array in the very end, after your outer loop. Commented Sep 16, 2019 at 23:56
  • Is there any way to do it without any external functions? Commented Sep 17, 2019 at 0:13
  • What do you mean by "external function"? Commented Sep 17, 2019 at 0:19

3 Answers 3

2

return breaks out of the function - have a result array instead:

function aperturas(arrayDeIngresosSemanales, cantMinEst) {
  let result = [];

  // ...

  if (a >= cantMinEst) {
    result.push(true);
  }
    result.push(false);
  }

  // ...      

  return result;

}

You could even remove the if statement:

result.push(a >= cantMinEst);
Sign up to request clarification or add additional context in comments.

Comments

0

You shouldn't return immediately after evaluating an array element.

Create a result array and push result of each array's evaluation and return the result.

Also, you aren't calling the function properly. First argument is an array of arrays, you have to call it like aperturas([[0, 0, 3, 0], [1, 2, 4, 5], [0, 0, -1]], 3)

function aperturas(arrayDeIngresosSemanales, cantMinEst) {

  // Create an array to store the result.
  var result = [];

  for (var i = 0; i < arrayDeIngresosSemanales.length; i++) {

    var a = 0;
    var arr = arrayDeIngresosSemanales[i];

    for (var j = 0; j < arr.length; j++) {
      if (arr[j] <= 0) {
        a = a + 1;
      }
    }
    
    // Now compare with limit after iterating completely over the array.
    if (a >= cantMinEst) {
      result.push(true);
    } else {
      result.push(false);
    }
  }

  // After iterating over all the arrays, return the result. 
  return result;
}

console.log(aperturas([[0, 0, 3, 0], [1, 2, 4, 5], [0, 0, -1]], 3));

Comments

0

Or, alternatively if you want to use a more semantic JS feature than the for loop, you could keep the return statements but use the map function instead. That would look something like this:

arrayDeIngresosSemanales.map((arr) => {
    var a = 0;

    for (var j = 0; j < arr.length; j++) {
        if (arr[j] <= 0) {
            a = a + 1;
        }
    }
    if (a >= cantMinEst) {
        return true;
    } else {
        return false;
    }
})

By that same token, you could also replace the inner for loop with a reduce, but that will be left as an exercise for the reader.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.