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.

9
  • 1
    do you have some data and the given and wanted result? Commented Feb 27, 2020 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") Commented Feb 27, 2020 at 10:56
  • 1
    map always returns an array, Commented Feb 27, 2020 at 10:57
  • 1
    The code works fine, including array.join(). Read about Array.map(). Commented Feb 27, 2020 at 10:57
  • 1
    Here's all you need return str.split("").map(c => +!/[a-m]/g.test(c)).join(""); Commented Feb 27, 2020 at 10:59

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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'

Comments

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"

Comments

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

1 Comment

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...
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'))

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.