0

I define an absorbing String simply as follows: When concatenated with any other String, the result is empty String (another absorbing element).

I know that probably such element doesn't exist natively in JavaScript, but I came up with a simple logic that simulate this

"_".repeat(str.length/str.length)+ str+ "_".repeat(str.length/str.length)

Example:

var str = "HP";
var result = "_".repeat(str.length/str.length)+ str+ "_".repeat(str.length/str.length);
[out]: "_HP_"
str = "";
var result = "_".repeat(str.length/str.length)+ str+ "_".repeat(str.length/str.length);
[out]: ""

The purpose is simply to have underscores besides String only when they exist.

This is very helpful when many String are concatenated with one separator, and that we would like to avoid If Else block-like code.

Is there a shorter form?

Curcuma_
  • 851
  • 2
  • 12
  • 37
  • 4
    Please show input and expected output – mplungjan Jun 12 '18 at 08:45
  • 1
    What should be the reason for `str.length/str.length`? It's either `1` all the time anyway or results in a "division by zero". – Capricorn Jun 12 '18 at 08:50
  • Not optimal code but it does the job: `"_".repeat(NaN)` gives empty String. At least when run on Chrome JS engine V 65.0 – Curcuma_ Jun 12 '18 at 08:51
  • Why not use a (helper) function? have you tried a ternary operation like `str ? "_"+str+"_":""`? They usually result in the "shortest" way of expressing if-else structures – Capricorn Jun 12 '18 at 08:53
  • 1
    One way to concatenate many strings with one separator while avoiding If Else blocks would be: `[str1, str2, str3, ...].filter(str => str).join(SEPARATOR)` – Freeman Lambda Jun 12 '18 at 08:53
  • @Capricorn I'm already using plenty if ternary operations in the embedding code, I would like to avoid, but it's a solution. – Curcuma_ Jun 12 '18 at 08:55
  • @Curcuma_ so does `"_".repeat(0)`, why `NaN`? (btw, `"_".repeat(!str)` gives one for zero and zero for non-empty) – Amadan Jun 12 '18 at 08:55
  • Anyway `str.length/str.length` is either NaN or 1, wich helps. `"_".repeat(!str)` is a nice and comprehensible! I think I will stick with that. – Curcuma_ Jun 12 '18 at 08:57
  • If your goal is to just have a super-short call for that "expression" in your code, it might be solution to extend the string object: https://stackoverflow.com/questions/30257915/extend-a-string-class-in-es6 (although it's debatable whether that's a good practice) – Capricorn Jun 12 '18 at 08:59
  • @Amadan Sorry I was wrong, `!str` always evaluates true – Curcuma_ Jun 12 '18 at 09:00
  • 3
    @Curcuma_ I think you are not describing your problem in this SO question. You are rather describing your solution approach. If you edit the question and write down your full problem (which led you to needing the concept of an "absorbing" string) we might be able to give you an even better solution. – Freeman Lambda Jun 12 '18 at 09:00
  • In JavaScript, `!str` evaluates to false for an empty `str`. But I wholeheartedly agree with @FreemanLambda - this is an XY problem. – Amadan Jun 12 '18 at 09:02
  • I asked if such element exists, and described its behavior. I was not sure if such an element exists in JS natively, then I said, I accept a solution that simulate this behavior, many were presented, and I still look for the best. – Curcuma_ Jun 12 '18 at 09:02
  • @Curcuma_ What Freeman is aiming at is for us not to answer an X/Y problem, e.g. you have an issue, you have a possible solution so you ask about your solution instead of the actual problem you are trying to solve – mplungjan Jun 12 '18 at 09:28
  • I didn't ask about my solution! I just posted what I came up with, which is a horrible try, a problem might have a better solution anyway ! – Curcuma_ Jun 12 '18 at 10:52

1 Answers1

2

You could use a logical AND && wich checks the string for truthyness.

function pad(string) {
    return string && '_' + string + '_';
}

console.log(pad("HP"));
console.log(pad(""));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392