0

I don't know why I have a comma when I use join method. Can anyone explain?

function maskify(cc) {
  var a = cc.slice(cc.length - 4, cc.length);
  var b = cc.slice(0, cc.length - 4);
  b = b.split("");
  for (var i = 0; i < b.length; i++) {
    b[i] = "#";

  }
  b.join("#");
  console.log(b + a);
}

maskify("sadasdasdasdasdasd");
// result : #,#,#,#,#,dasd

2
  • yes i mean comma
    – duy khanh
    Apr 26, 2017 at 4:56
  • 1
    you are doing b.join(""#"); but not storing it anywere Apr 26, 2017 at 5:01

4 Answers 4

2

Join() is the method of array which allows to join its element as string and returns. Join() takes one argument which is separator, the default value is ",". You need to specify empty string if you want to join without any separator. For reference refer MDN.

For your code, you are joining but not storing, and again converting it to string by using + operator with variable a

    function maskify(cc) {
      var a = cc.slice(cc.length - 4, cc.length);
      var b = cc.slice(0, cc.length - 4);
      b = b.split("");
      for (var i = 0; i < b.length; i++) {
        b[i] = "#";
    
      }
      var output=b.join("#");
      console.log(output + a);
    }
    
    maskify("sadasdasdasdasdasd");
    // result : #,#,#,#,#,dasd

1

When you use split on b, b is an array of individual characters. When logging b + a, since b is an array and a a string, + will act as string concatenation operator. b is converted to string using toString() implicitly. toString on array returns string of array elements joined by ,. So, you're getting comma in the final output.

Simple solution to solve this problem is to explicitly join the array elements before concatenating.

function maskify(cc) {
  var a = cc.slice(cc.length - 4, cc.length);
  var b = cc.slice(0, cc.length - 4);
  b = b.split("");
  for (var i = 0; i < b.length; i++) {
    b[i] = "#";

  }
  b.join("#");
  console.log(b.join('') + a);
}

maskify("sadasdasdasdasdasd");

Another way to achieve same results is using repeat

var str = 'sadasdasdasdasdasd';

var maskedStr = '#'.repeat(str.length -4) + str.slice(-4);
console.log(maskedStr);

Note that this is supported in latest browsers/environments only. Check the browser compatibility, if not supported use polyfill.

1

It's because when concatinating an array and a string, the array is implicitly converted into a string.

var array = [...];
array + "";

is the same as:

array.toString() + "";

and [4, 5].toString() is "4,5" (add a , by default).

Why?

Because, when this line is reached:

console.log(b + a);

a will be a string because it was cut from the string cc. And b is an array because it is the result of the split, and b.join don't change b to a string, it just return a string that you don't use and leaves b intact (so b remains an array).

Fix:

Use the result of b.join not b:

console.log(b.join("#") + a);

or:

b = b.join("#");
console.log(b + a);
0

i add

var c = b.concat(a);
c.join("");

It's working . Thanks u so much :D

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.