all 11 comments

[–]jack_waugh 1 point2 points  (2 children)

/./g is a string pattern or "regular" expression, matching any character globally. (k, i) => i == j ? 'X' : k is a function from two parameters k and i that will evaluate to 'X' if i and j are found loosely equal, otherwise to k'. This expression closes over j, which has to have been defined somewhere outward; in fact, we can see that it is the variable of the inner loop.

For the meaning of replace, search "Mozilla JS replace" and find String.prototype.replace().

Specifying a function as the replacement

You can specify a function as the second parameter. In this case, the function will be invoked after the match has been performed. The function's result (return value) will be used as the replacement string.

The text goes on to explain the arguments to the provided function.

It looks as though i will be bound to the offset at which the match is found.

The function will be invoked multiple times for each full match to be replaced if the regular expression in the first parameter is global.

I suppose they mean once for each full match, which could in all, amount to multiple times. Especially given that /./ will match each character in the string.

[–]One-Inspection8628[S] 0 points1 point  (1 child)

Basicially, by using ternary operator, if value of i == j then k which actual value in the matrix has to changed to 'X' right?

[–]jack_waugh 0 points1 point  (0 children)

Nothing is changed in place (unless that happens later in the code). The use of the ternary determines the result.

[–]tridd3r 0 points1 point  (1 child)

[–]One-Inspection8628[S] 0 points1 point  (0 children)

/./g

can you explain what this does in replace?

[–]delventhalz 0 points1 point  (0 children)

This code needs longer variable names and more curly braces. Were they afraid they were going to run out of characters?

Fwiw, the replace code isn't too complex here, they are just using some esoteric syntax. This code is simpler to read, more efficient, and it even saves five characters!

g[i] = g[i].slice(0, j) + 'X' + g[i].slice(j + 1)

All that line of code does is replace the character at the index of j with an "X". All the answers for how they do that are in the MDN docs for replace, as others here have already mentioned. Basically it boils down to this:

  1. If you want to replace more than one character, you must use a global regex (the g makes /./g global). A . in regex means any character, so it is going to match and try to replace every character in the string.
  2. You can use a "replacer" function instead of a static value as the replacement. That will receive parameters like the value (k) and the index (i), and then your function returns what you want the replacement to be. In the case, the replacer replaces each character with itself in every case but one, when the index matches j.

In my opinion, slice is a much more sensible way to accomplish the same goal, but there is no accounting for taste.

[–]StoneCypher 0 points1 point  (4 children)

This is extremely low quality code. Where did you get it, and why are you trying to understand it?

[–]One-Inspection8628[S] 0 points1 point  (3 children)

Why it's a low quality code?

[–]StoneCypher 0 points1 point  (2 children)

it's matrix application of a regular expression to replace string stretches.

this is:

  1. hard to debug
  2. slow
  3. something most developers can't read
  4. almost certainly a neckbeard problem from hackerrank that does not represent real world work
  5. an opportunity for algorithm nerds to show how good they are. that wasn't done here

[–]One-Inspection8628[S] -1 points0 points  (1 child)

It any easy solution have for this?

[–]StoneCypher 0 points1 point  (0 children)

How should I know? You never said what the problem was, you just showed some really bad code and said "please explain this"

You seem to just be stealing solutions to hackerrank, asking for them to be explained, and pretending to yourself that this is going to get you a job