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 return boolean in an Array.prototype function?

Im trying to maken a function to use the AND-Logicgate but it only respons an array.
I tryed useing toString() and join() but they dont work. Can anybody help me.

Here is the Code:

Array.prototype.AND = function() {
  var temp = true;
  for (let i = 0; i < this.length; i++) {
    if (this[i] == false) {
      temp = false;
    }
  }
  while (this.length > 0) {
    this.pop();
  }
  this[0] = temp;
}

const test = [true, true, true, false];

test.AND();

console.log(test);

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 :

So if the array contains a false, the prototyped function .AND should return false.

Use some to determine if the array contains a false.

const test = [true, true, true, false];
const test2 = [true, true, true, true];

Array.prototype.AND = function() {
  return !this.some(item => item == false)
}

console.log(test.AND())   // false
console.log(test2.AND())  // 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