Binary Agents Confusion

Tell us what’s happening:
Hi, everyone,

I’m working to better understand the implications of this challenge and solving it bit by bit and I came across something odd.

My question is, how can console.log(strToArr[4].match(regexTest).length); return 1 when that string (00100111) begins with two 0’s?

Thank you!

Your code so far

function binaryAgent(str) {
  var strToArr = str.split(' ');
  var numbers = [];
  var regexTest = /^0+/;


  var eliminateZeroes = function(array, newArray) {
    for (var i = 0; i < array.length; i++) {
      if (array[i].match(regexTest) != "") {
  eliminateZeroes(strToArr, numbers);

binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36.

Link to the challenge:

Because the output is an array [“00”] which has a .length of 1.
You can check this in the browser console.

You can do something like:
to turn it into a string then get the length.

Hi, Jesse,

Thanks a lot, that helped!

Any idea why the output of that regex test is an array?
Shouldn’t it return a string since the test is applied on a string?

“The match() method searches a string for a match against a regular expression, and returns the matches, as an Array object.”

Always an array. (I think - I’m still learning =) )

Match is an exploratory method which goes over the entire string looking for portions of the string that match the given pattern and returns a special kind of array that provides you with all the matches, the indices at where they were found and the exact match plus capturing groups.

Since it only found one match it returns a list of exactly 1 match.

That makes sense, I guess I skipped the match() definition.

Again, thank you!

1 Like