Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get an array of booleans as answer for a parameter "under 10"?

I want to evaluate whether the number is under than 10, having as an answer an array of booleans.
I tried using forEach, map, but I didn’t succeed in any attempt.

function underTen (arr){
  let underTenAnswer = arr.forEach(function(n){
    if (n < 10) {
      true;
    } else {
      false;
    }
  })
}

Input: underTen([1,9,12,19,4,16]
Expected outcome: Array [True, True, False, False, True, False]

I’m a beginner, sorry if something is notoriously incorrect.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There is a really quick way to do this:

let a = [1, 2, 3, 4, 5, 6, 7];
let b = [1, 2, 3, 4, 12, 8];

console.log(a.every((x) => x < 10)) // true
console.log(b.every((x) => x < 10)) // false

We use Array.every() here to check if each array element passes some condition.

If you need to return an array of booleans, then you can use Array.map():

let b = [1, 2, 3, 4, 12, 8];
console.log(b.map((x) => x < 10)) // [true, true, true, true, false, true]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading