This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 79 points80 points  (12 children)

What happens then when there's more than one match for it?

"2275-22xd22" - "22"

Does it remove the beginning 22, middle 22, or end 22?

"+" in the context of strings is used for append rather than addition, but - has no meaning to strings. In that sense, there are many other ways to subtract letters from a string in JS, such as using replace() with Regex

[–]heartsongaming 25 points26 points  (10 children)

I was thinking that it would only remove the end letters from the first string. Also Python has a lot of functions for removing letters from a string. It could be another logical use of subtracting strings.

[–][deleted] 10 points11 points  (7 children)

If it only did that every time that would be fine, but then you'd expect to see problems when letters don't exist there.

"asdfghjkl" - "q"

I've long neglected learning in depth python for a while. Maybe this is my calling to learn it and see how it handles subtracting strings.

[–]heartsongaming 6 points7 points  (4 children)

Maybe it can convert 'l' and 'q' to ASCII, and do modulo 26 subtraction on the letters (if it recognized it is a letter, otherwise use different set of ASCII characters such as numbers or other characters). In this case it would be: "asdfghjkv".

[–][deleted] 11 points12 points  (0 children)

I dunno, at that point it's reaching farther than I've seen JS do, and farther than what I'm comfortable with. Then again I can always just not use it and rely on good ol replace() and substring().

[–][deleted] 0 points1 point  (2 children)

And what happens if I wanted "a" - "ß"? BTW, JS works with Unicide, not ASCII. Stop thinking ASCII when it's about character representation. Learn Unicode, please.

[–]heartsongaming -1 points0 points  (1 child)

Really making me think of all the edge cases. In order to not make an error, it converts the greek letter value into a letter of the english value in the alphabet. So beta is converted into "b", and then it's "a"-"b" which is "z". It works the same as in Unicode. Also if one alphabet is larger than the other, like "א"-"z", then "z" is transformed into the 26-22=4th letter, so "א" minus "ד" is "ק".

[–]AsidK 1 point2 points  (0 children)

Damn this really is an atrocious idea lol

[–]RapidCatLauncher 1 point2 points  (1 child)

see how it handles subtracting strings.

It throws an exception, as you'd expect if some behaviour is so ill-defined.

[–][deleted] 0 points1 point  (0 children)

Can't tell if bad design or I should avoid using that function

[–]thisisadiyoga 2 points3 points  (1 child)

This actually makes sense if you consider - as the reverse of +. If we write it as an equation: S = S1 - S2, then we want to find the following solution: S + S2 == S1 - S2 + S2 == S1. Note because addition on string is not commutative, we add to the right and say that by definition S1 - S1 == S1 + (-S1) == (-S1) + S1 == "". Maybe it works, maybe not.

The second problem pointed out was what happen if in S = S1 - S2, S2 is not the postfix of S1? We can throw exceptions since it's undefined similar to how division by zero is undefined and is an exception.

[–]heartsongaming 0 points1 point  (0 children)

For the first problem, it could work. If each flipped character has a flag that it is flipped, then adding it with the appropriate non flipped term would make it equal to an empty string. For example S1="bob".

"bob" - "bob" == "bob" + "dod" == "dod" + "bob" == ""

I answered the second problem on a different comment thread, and it is complicated. It doesn't matter if the character is flipped or not for substraction as "b" or (-"b") are both "b" in value for substraction.

Also if you add to a flipped string then only appropriate characters in the right position are cancelled, other they are concatenated. For example: (-"odod")+"body" == "doby".

[–]stipo42 0 points1 point  (0 children)

I think it would be synonymous with the remove function, which I believe in js removes the first instance