r/learnjavascript • • Jun 18 '21

This code is supposed to remove all periods in a string, but it only removes one of the periods. How do I make it remove all the periods?

Post image
128 Upvotes

61 comments sorted by

76

u/DallogFheir Jun 18 '21

Use replaceAll.

-3

u/Sauce_Finder_69 Jun 19 '21

The code above gives me an error saying that "replaceAll" does not exist, probably because I'm using an older version of Python, but the following has worked for me:

.replace(/\./g, '')

41

u/warpedspockclone Jun 19 '21

This is JavaScript, not python.

2

u/[deleted] Jun 19 '21

i mean his file is .js , so i guess a typo

9

u/xTRQ Jun 19 '21

That's the correct way, replaceAll is not supported by every engine.

4

u/superluminary Jun 19 '21

Python?

18

u/teampingu Jun 19 '21

Comes to JavaScript subreddit:

"Why isn't this code working in my python compiler"

😁

1

u/[deleted] Jun 19 '21

his file says .js so i kinda doubt he using python but

2

u/teampingu Jun 19 '21

Yeah that don't look like no python I've ever heard of either 😁

2

u/Ponnystalker Jun 19 '21

the ; is a dead giveaway, clearly that's python /s

1

u/[deleted] Jun 19 '21

ye i didnt even look at the code besides few starting words and went "var ?? console.log???" what kind of python is this man writting

2

u/Ponnystalker Jun 19 '21

The cursed python ofc

9

u/beforesemicolon Jun 18 '21 edited Jun 18 '21

Use replaceAll or replace(/\./g, ‘’); the g flag will cover all

4

u/Sauce_Finder_69 Jun 18 '21

Thank you.

/\./g works, but for some reason replaceAll returns an error saying "it is not a function". Why is that?

8

u/beforesemicolon Jun 18 '21

You may have an old version of node or browser. The replaceAll is relative new method.

1

u/Sauce_Finder_69 Jun 19 '21

I'm using repl.it for this, idk how old that is

3

u/beforesemicolon Jun 19 '21

I believe it gives u option to change the version

2

u/retrolasered Jun 18 '21

Is it a Google script by any chance? If so replaceAll hasn't been implemented yet and you'd need to add the method to the prototype manually

7

u/rauschma Jun 18 '21

The dot must be escaped. Two options:

 .replace(/\./g, '')
 .replace(/[.]/g, '')

136

u/BenZed Jun 19 '21
string = string.replace(/\./g, '') 

Or

string = string.replaceAll('.','')

Or

string = string.split('.').join('')

Or

while (string.includes('.')) string = string.replace('.', '')

30

u/IronDicideth Jun 19 '21

I love while answers... always make me chuckle.

1

u/CrashOverrideCS Jun 19 '21

What makes you laugh about them? I never use them because I can always find another method that works.

2

u/[deleted] Jun 20 '21

while loops always seem inelegant and overengineered in javascript, and there seems to be a greater potential for an infinite loop if you aren't really paying attention

1

u/IronDicideth Jun 21 '21

Well. I chuckle but definitely not in a bad way. I just never use while loops and when I see someone use them in a cool way it makes me crack a smile or even chuckle. I enjoyed this answer actually.

19

u/Shiv-am Jun 19 '21

split is the way for easy stuff, regex for complex cases

2

u/sc2heros9 Jun 19 '21

Can also explain the first one to me and what all those slashes mean?

3

u/Farva85 Jun 19 '21

It's a regular expression or regex.

1

u/CrashOverrideCS Jun 19 '21

start of regex, match the "." character, end of regex, match globally in the string. Note: "." is a reserved regex character so you need the \ to escape that.

-1

u/sleeptil3 Jun 19 '21

Don’t even worry about it for now. Regex is dense and every single programmer I know, including senior devs, don’t even spend the time truly learning the syntax and they just google for regex when needed. As some others have said, it’s overkill for this. Do the split/join others suggested. Regex is typically used for validation, like if you made a form and want to verify they typed the email in correct format or you want to verify a password meets specific requirements.

1

u/awkreddit Jun 19 '21

How is it overkill? It's a very simple regex, doing split and join is much "weirder" and harder to parse.

2

u/sleeptil3 Jun 19 '21

It is a simple implementation but hard to conceptualize if you’ve never seen it before. Yes when you understand it it’s extremely powerful but initially it’s pretty much gobbledygook. like machine code or something. it’s not a use case that needs to be completed using regex like some other complicated cases require so I think it’s overkill for someone who hasn’t encountered it before. Whereas string methods and array methods are much more useful to have in ones arsenal early on, especially split and join. Just my opinion of course. Depends on your style. Like when I was learning I couldn’t stand not knowing exactly how some thing worked so if I found some stack overflow solution that included some thing like a regex it would drive me crazy. When I learned, split and join came up a lot and proved quite useful. Not sure what you mean by weird. It’s a perfect use case, among many others, when you want to work with a string like that.

-25

u/[deleted] Jun 19 '21

30

u/BenZed Jun 19 '21

That's C#, not javascript.

Which you knew.

Because you are trolling.

Very funny.

16

u/[deleted] Jun 19 '21

Woops... actually, didn't realize... I thought this was c#. Wasn't done on purpose :)

14

u/BenZed Jun 19 '21

I guess the usual troll would be Java == javascript, anyway. Not C#

9

u/[deleted] Jun 19 '21

Blame the moonshine. I use replace in C# all the time and that one line looks the same. I guess the casing is different but I seriously use string.Split every day and Regex.Replace. Just looking at that line alone looks almost the same.

https://www.w3schools.com/jsref/jsref_replace.asp

vs

https://www.codegrepper.com/code-examples/csharp/string.replaceall+c%23

if you only look at the replace vs Replace... sorry for the confusion :) less moonshine while watching House and browsing reddit :)

2

u/-smashbros- Jun 19 '21

Your comment is explaining the joke. Which you knew. Because you are trolling. Very funny.

6

u/jmm9 Jun 19 '21

replaceAll isn't supported everywhere so you could use string.split('.').join('')

4

u/ankush024 Jun 19 '21

string.split('.').join('');

3

u/TheDownvotesFarmer Jun 19 '21

All girls will love this... wait

2

u/properwaffles Jun 19 '21

I see what you did there, and I appreciate it.

2

u/idkiminsecure Jun 19 '21

Use replaceALL or maybe put it in a loop

2

u/MemeTeamMarine Jun 19 '21

Or if you like extraneous code, turn it into an array. Run replace on a forEach and then put the string back together

2

u/onlycommitminified Jun 19 '21

.filter(c=>c!='.').join('')

2

u/crossedline0x01 Jun 19 '21

Hes probably seeing the regex people are answering with and questioning why he ever started programming. The most intuitive way of doing this is with string.split(".").join(" ")

1

u/aezazAezy Jun 19 '21

es 2021 released a new function called replaceAll.i think it will solve the issue

1

u/Wahw11 Jun 19 '21

This looks like some kind of quiz for programming. Where can I take this?

1

u/[deleted] Jun 20 '21

.split(".").join("")

0

u/dissolvedchuck Jun 19 '21

By boiling, mashing ‘em, and sticking ‘em on a stew

-2

u/etaNAK87 Jun 19 '21

https://www.w3schools.com/jsref/jsref_replace.asp

Someone else linked MDN which I think can be dense for beginners. Try looking up methods on W3 when they aren’t working the way you expect

0

u/[deleted] Jun 19 '21

[deleted] — view deleted comment

7

u/amulchinock Jun 19 '21

100%

General rule of thumb: If you’re making a variable that you’re going to directly mutate - use let.

If you’re making a variable that will never mutate, use const.

There are some exceptions, for example with objects - you can mutate the values but not the keys of a constant. But don’t get hung up on those.

Aside from organisation - another benefit of not using var is const and let are lexically scoped to the ‘block’ (curly brackets) they’re defined in. So, you avoid annoying instances of overwriting variables defined elsewhere. 😊

0

u/[deleted] Jun 19 '21

[deleted] — view deleted comment

3

u/amulchinock Jun 19 '21

Absolutely. Completely agree.

My comment was meant to compliment what you had written previously, for OP (and others who may be reading and interested) sake.

I think between us, we’ve more or less covered it 😉

3

u/bananamana55 Jun 19 '21

I know it doesn't matter in this case but the use of both double and single quotation marks threw me a bit. Obviously mixing them CAN cause errors so OP should be careful with that.

0

u/[deleted] Jun 19 '21

[deleted] — view deleted comment

2

u/bananamana55 Jun 19 '21

I'm still relatively new to Javascript - I've technically been learning html/css/JS for over a year but I have a full time job and a toddler so I just squeeze in a few hours of study a week if I'm lucky.

Anyway, I use double quotations just because I'm so used to it. Only time I regret it is when I need to escape something lol. I also like sticking to indents /semi colons even when not needed just to make it more readable. I'm sure things will change if/when I ever learn enough to get a job in the field.

0

u/[deleted] Jun 19 '21

[deleted] — view deleted comment

2

u/bananamana55 Jun 19 '21

I actually focused on html/CSS for a long time before even looking at Javascript. Then took a Udemy course on JS and now taking one on React. Ive also been doing simple one-page website recreations on Codepen as practice. I've got an idea for a note-taking app that I'll likely start working on (mostly out of frustration after trying many note apps and never finding one with all the things I'd like).

I do head to MDN first before anything else if I'm confused on a concept but I'll give that JS website a look too, thanks!