Could someone help me with this JavaScript, I'm trying to make an encoder but it doesn't work correctly. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Could someone help me with this JavaScript, I'm trying to make an encoder but it doesn't work correctly.

I'm trying to make an utf8 to ascii converter but there are a few bugs in it. ###code removed because question is answered### 1. if the same letter appears 2+ times in a string it only replaces the first one. 2. it changes the already replaced letters. example 1 = %31 but the script changes the 3 from %31 also creating %%331 and than the url isn't working anymore. thanks for helping me. more about the encoding: https://www.w3schools.com/tags/ref_urlencode.asp

26th Feb 2017, 7:12 PM
Melvin2204old
Melvin2204old - avatar
2 Answers
+ 4
1> String.replace() will replace only the first occurence by default. To replace all occurences, you must pass a regular expression at first argument, with the 'g' flag... 2> Your replacement logic fail, because in your replacement chain, you are replacing some stuff that's already a replacement stuff. To fix that, I suggest to change totally your algorithm, by iterating through the input string, and storing in another variable the result of the conversion ( so in addition, you can improve your conversion step by using a key-value object structure ^^ )...
26th Feb 2017, 8:43 PM
visph
visph - avatar
0
@visph thanks it is working now.
26th Feb 2017, 10:40 PM
Melvin2204old
Melvin2204old - avatar