lois v
lois v

Reputation: 21

Invert an array of booleans in JavaScript

I am trying to invert the values of a string of boolean values so that instead of

inverted([true, false, true, false, true])

it is [false, true, false, true, false]

So far I have come up with this:

function inverted(bools) {
    var inversion = [];
    var string= [];
    while(!bools) {
      string.push(bools);
    }
    return inversion;
}

But my code is not working, any and all help will be much appreciated!

Upvotes: 2

Views: 4073

Answers (8)

Guilherme Oliveira
Guilherme Oliveira

Reputation: 43

let myArray = [true, false, false, true]
// single line solution:
let invertedArray = myArray.map((bool) => !bool)

console.log(invertedArray)

Upvotes: 0

gyre
gyre

Reputation: 16779

Though many answers here are already great, I thought I would chip in my "golfed" regex solution:

function inverted (b) {
 return b.map(/f/.test, /f/)
}

console.log(
  inverted([true, false, true, false])
)

Upvotes: 1

kourouma_coder
kourouma_coder

Reputation: 1098

This is another way to achieve that :

var booleans = [true, false, true, false, true];

function inverted(bools) {
    var inversion = [],i=0;

    while(i< bools.length) {
      inversion.push(!(bools[i]));
      i++;
    }
    return inversion;
}

var inverted =  inverted(booleans);

console.log(inverted);//Outpu : [false, true, false, true, false]

Upvotes: 0

Joe H
Joe H

Reputation: 1

You need to split up bools with bools.split(). Then you can check and invert them. You also are not putting anything in inversion[] prior to returning it. Something like the below should work:

function inverted(bools) {
var inversion = [];
var string= bools.split(",");
for(var i=0; i<string.length(); i++) {
  inversion.push(string[i]);
}

return inversion;

}

Upvotes: 0

Robby Cornelissen
Robby Cornelissen

Reputation: 97130

You can do it like this:

const booleans = "[true, false, true, false, true]";
const inverted = JSON.stringify(JSON.parse(booleans).map(b => !b));

console.log(inverted);

If by a "string", you actually mean an array, you don't need to convert to/from a string and can just do:

const booleans = [true, false, true, false, true];
const inverted = booleans.map(b => !b);

Upvotes: 1

Zibx
Zibx

Reputation: 94

Your code is just one big mistake. You do not understand basic principles of working with arrays. Do not just use my solution, please read some articles about iterating and getting an element from an array. This one should work:

function inverted(bools) {
  var i, _i, inversion = [];

  for(i = 0, _i = bools.length; i < _i; i++){
    inversion[i] = !bools[i];
  }
  return inversion;
}

Upvotes: 1

Hari Lamichhane
Hari Lamichhane

Reputation: 530

function inverted(bools) {
  for(var i = 0; i < bools.length; i++) {
     bools[i] = !bools[i];
   }
  return bools;
}

Use it as

var bools = [true, false, true];
bools = inverted(bools);

Upvotes: 1

guest271314
guest271314

Reputation: 1

You can use .map(), ! operator to invert Boolean value, return array of results

function inverted(bools) {
   return bools.map(function(bool) {return !bool})
}

Upvotes: 6

Related Questions