0

I'm confused about why array.join("") is not working properly in this function.

If the letter is a-m it will show 0 and the otherwise is 1 convertBinary("house") ➞ "01110"

function convertBinary(str) {
  var jack=str.split("")
  return jack.map(function(e) {
    var array=[]
    if(e.match(/[abcdefghijklm]/g)) {
      array.push(0)
    } else {
      array.push(1)
    }
    return array.join("");
  })
}

Where did I go wrong.

Fletcher Rippon
  • 1,837
  • 16
  • 21
UberSelf
  • 15
  • 6
  • 1
    do you have some data and the given and wanted result? – Nina Scholz Feb 27 '20 at 10:54
  • Everything inside the function in the map is run for *each item* in the jack array. Also, map is designed to transform each item. You likely want to transform each item into 0 or 1, and so you should change your code to `return 0` or `return 1`. The map function will return the new array, so you should add `join("")` to the result of the map function. (NB - Your array inside the function inside map is not necessary - your code in this function just results in "0" or "1") – David E Feb 27 '20 at 10:56
  • Why even have `array` in the first place? The parameter `e` is a single character, so the `map()` callback should return zero or one, not an array. Then you join at the end of the main function, not inside the map callback. –  Feb 27 '20 at 10:56
  • @NinaScholz I updated it in the post thanks – UberSelf Feb 27 '20 at 10:57
  • 1
    `map` always returns an array, – Teemu Feb 27 '20 at 10:57
  • 1
    The code works fine, including `array.join()`. Read about [`Array.map()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map). – axiac Feb 27 '20 at 10:57
  • 1
    Here's all you need `return str.split("").map(c => +!/[a-m]/g.test(c)).join("");` –  Feb 27 '20 at 10:59
  • Get it thanks guys, just noobie here, appereciate everyone's help :D – UberSelf Feb 27 '20 at 11:03
  • BTW, you don't need `split` & `join`. You can use `replace` as `str.replace(/([a-m])|([^a-m])/g, (m, $1) => $1 ? 1 : 0)`. – Tushar Feb 27 '20 at 11:05

6 Answers6

1

You need to apply the .join("") to the result of the map() call, instead of inside the map() inner function, which also should return just 0 or 1 for each character.

Also it would be better to use regex.test(str) because we are only interested in if there is a match (true or false), see e.g. what is the difference between .match and .test in javascript.

Like this:

function convertBinary(str) {
  var jack = str.split("")
  return jack.map(function(e) {
    if (/[abcdefghijklm]/.test(e)) {
      return 0;
    } else {
      return 1;
    }
  }).join("");
}

console.log(convertBinary("house"));
Peter B
  • 22,460
  • 5
  • 32
  • 69
0

Initialize array and return array.join() outside the map function:

function convertBinary(str) {
      var jack=str.split("");
      var array = jack.map(function(e){
        
        if(e.match(/[abcdefghijklm]/g)){
            return 0;
        } else {
            return 1;
        }
     })
     return array.join("");
    }
    console.log(convertBinary('house'));
Saurabh
  • 429
  • 3
  • 10
  • 1
    If you're not using the return value you should use `forEach` instead of `map`. However declaring an empty array and filling it inside `forEach` is *exactly* why `map` exists... –  Feb 27 '20 at 11:02
0

The array.join() must be outside the jack.map() function, like this:

var jack_bin = jack.map(function(e)
{
    var array=[]
    if(e.match(/[abcdefghijklm]/g))
    {
        return '0';
    }
    else
    {
        return '1';
    }
})
return jack_bin.join("");
Asenar
  • 6,732
  • 3
  • 36
  • 49
0

The key point here is that it should be fixed as jack.map(...).join(), because jack.map() returns an array.

function convertBinary(str) {
    var jack=str.split("")
    return jack.map(function(e)
    {
        var array=[]
        if(e.match(/[abcdefghijklm]/g))
        {
            array.push(0)
        }
        else
        {
            array.push(1)
        }

        return array;
    }).join("")
    }
    
console.log(convertBinary("house")); // output is '01110'
Alexander
  • 77
  • 1
  • 1
  • 6
0

Please try this solution

function convertBinary(str) {
      const jack = str.split('');
      const binArr =  jack.map(e => {
        if (e.match(/[abcdefghijklm]/g)) {
          return 0;
        } else {
          return 1;
        }
      });
      return binArr.join('');
    }
    
    
    console.log(convertBinary('house'))
Not A Bot
  • 2,474
  • 2
  • 16
  • 33
g8bhawani
  • 674
  • 4
  • 8
0

You could short it a bit by using a chain of methods and use test instead of match, because you need a boolean value, not an array of matched items/null.

Then you need to take eiter 1 or 0 and join the result for getting a string.

function convertBinary(string) {
    return string
        .split("")
        .map(function(e) {
            return /[abcdefghijklm]/.test(e) ? 1: 0;
        })
        .join('');
}

console.log(convertBinary("house")) // "01110"
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392