2

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!

1
  • 1
    to be frank there are obvious issues in your code - you update string variable but return inversion array in the end. Also you assume and iterate with a truthy condition. You need to iterate on the array and push an inversion on the boolean. Commented Mar 31, 2017 at 1:31

8 Answers 8

6

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

function inverted(bools) {
   return bools.map(function(bool) {return !bool})
}
Sign up to request clarification or add additional context in comments.

5 Comments

If input is actually "[true, false, true, false, true]", inverted("[true, false, true, false, true]".match(/true|false/g).map(eval)) or inverted("[true, false, true, false, true]".match(/true|false/g).map(function(bool){return bool === "true"}))
You could avoid the need for eval using a look-ahead: inverted("[true, false, true, false, true]".match(/true|(?=false)/g).map(Boolean))
@gyre "[true, false, true, false, true]".replace(/(true)|(false)/g, function(match, p1, p2) {return p1 ? false : true})
Both valid options, just thought it was worth mentioning :)
@gyre Yes, interesting generation and use of empty string at your approach.
1
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);

Comments

1

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;
}

4 Comments

@RobbyCornelissen, I usually optimize my code for performance. My code with for is 12 times faster in Chrome than your code with map. jsperf.com/inverting-an-array
@RobbyCornelissen The previous comment could be two comments. The first including the first two sentences, the second the remainder of sentences; in order to not detract from the former. Or, only first two sentences, omitting remainder of comment.
@guest271314 You're right. I think that the tone of the first two sentences of this answer put me in a bit of an indignant mood. I'll rewrite my comment.
@Zibx Point taken, my comment was out of line. On Chrome your code indeed runs 12 times as fast and on Firefox almost twice as fast.
1

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);

Comments

1

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])
)

Comments

0

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;

}

Comments

0

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]

Comments

0

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

console.log(invertedArray)

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.