Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » Node.Js encode/decode
- pokeshah
-
Scratcher
100+ posts
Node.Js encode/decode
I'm new to JS, and I need help with encoding and decoding between a repl.it and a scratch project. String2Num doesn't work
String2Num:

String2Num:

Last edited by pokeshah (July 8, 2021 00:38:50)
- Raihan142857
-
Scratcher
1000+ posts
Node.Js encode/decode
not sure what “StringToNum” is but
const CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`1234567890-=~!@#$%^&*()_+[]\;\',./{}|:"<>? '; const encode = (text) => [...text].map((char) => CHARS.indexOf(char).toString().padStart(2, '0')).join(''); const decode = (digits) => digits.toString().split(/(?=(?:..)*$)/).map((idx) => CHARS[idx]).join('');
Last edited by Raihan142857 (July 8, 2021 02:34:08)
- gosoccerboy5
-
Scratcher
1000+ posts
Node.Js encode/decode
huh, that doesn't work with spaces.const CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`1234567890-=~!@#$%^&*()_+[]\;\',./{}|:"<>?'; const encode = (text) => [...text].map((char) => CHARS.indexOf(char).toString().padStart(2, '0')).join(''); const decode = (digits) => digits.toString().split(/(?=(?:..)*$)/).map((idx) => CHARS[idx]).join('');
decode(encode("hello world")); //helloworld
const CHARS = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`1234567890-=~!@#$%^&*()_+[]\;\',./{}|:"<>?'; const encode = (text) => [...text].map((char) => CHARS.indexOf(char).toString().padStart(2, '0')).join(''); const decode = (digits) => digits.toString().split(/(?=(?:..)*$)/).map((idx) => CHARS[idx]).join('');
Last edited by gosoccerboy5 (July 8, 2021 02:27:28)
- Raihan142857
-
Scratcher
1000+ posts
Node.Js encode/decode
oops, thought I fixed thathuh, that doesn't work with spaces.const CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`1234567890-=~!@#$%^&*()_+[]\;\',./{}|:"<>?'; const encode = (text) => [...text].map((char) => CHARS.indexOf(char).toString().padStart(2, '0')).join(''); const decode = (digits) => digits.toString().split(/(?=(?:..)*$)/).map((idx) => CHARS[idx]).join('');unless that's on purpose, a simple fix would probably be to add a space character to the CHARS variable.decode(encode("hello world")); //helloworldconst CHARS = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`1234567890-=~!@#$%^&*()_+[]\;\',./{}|:"<>?'; const encode = (text) => [...text].map((char) => CHARS.indexOf(char).toString().padStart(2, '0')).join(''); const decode = (digits) => digits.toString().split(/(?=(?:..)*$)/).map((idx) => CHARS[idx]).join('');
lol you tested it on the exact same string as I did
Last edited by Raihan142857 (July 8, 2021 02:34:41)
- pokeshah
-
Scratcher
100+ posts
Node.Js encode/decode
idk why buthuh, that doesn't work with spaces.const CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`1234567890-=~!@#$%^&*()_+[]\;\',./{}|:"<>?'; const encode = (text) => [...text].map((char) => CHARS.indexOf(char).toString().padStart(2, '0')).join(''); const decode = (digits) => digits.toString().split(/(?=(?:..)*$)/).map((idx) => CHARS[idx]).join('');unless that's on purpose, a simple fix would probably be to add a space character to the CHARS variable.decode(encode("hello world")); //helloworldconst CHARS = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`1234567890-=~!@#$%^&*()_+[]\;\',./{}|:"<>?'; const encode = (text) => [...text].map((char) => CHARS.indexOf(char).toString().padStart(2, '0')).join(''); const decode = (digits) => digits.toString().split(/(?=(?:..)*$)/).map((idx) => CHARS[idx]).join('');

- Chiroyce
-
Scratcher
1000+ posts
Node.Js encode/decode
idk why but
a simple fix would probably be to add a space character to the CHARS variable.
- pokeshah
-
Scratcher
100+ posts
Node.Js encode/decode
mhmmmmit isidk why buta simple fix would probably be to add a space character to the CHARS variable.const RESULTMAXSIZE = 3000; const CHARS = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`1234567890-=~!@#$%^&*()_+[]\;\',./{}|:"<>?'; const encode = (text) => [...text].map((char) => CHARS.indexOf(char).toString().padStart(2, '0')).join(''); const decode = (digits) => digits.toString().split(/(?=(?:..)*$)/).map((idx) => CHARS[idx]).join(''); console.log(decode(encode('The quick brown fox jumps over the lazy dog')))
- Raihan142857
-
Scratcher
1000+ posts
Node.Js encode/decode
simple fix
const CHARS = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`1234567890-=~!@#$%^&*()_+[]\;\',./{}|:"<>?'; const encode = (text) => [...text].map((char) => CHARS.indexOf(char).toString().padStart(2, '0')).join(''); const decode = (digits) => digits.toString().split(/(?=(?:..)*$)/).map((idx) => CHARS[idx.replace(/^0/, '')]).join('');
- pokeshah
-
Scratcher
100+ posts
Node.Js encode/decode
const CHARS = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`1234567890-=~!@#$%^&*()_+\;\',./{}|:"<>?';Thank you!
const encode = (text) => .map((char) => CHARS.indexOf(char).toString().padStart(2, ‘0’)).join('');
const decode = (digits) => digits.toString().split(/(??:..)*$)/).map((idx) => CHARS).join('');
also is there a scratch version
- Raihan142857
-
Scratcher
1000+ posts
Node.Js encode/decode
literally just search “encode” and there'll be a million resultsconst CHARS = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`1234567890-=~!@#$%^&*()_+\;\',./{}|:"<>?';Thank you!
const encode = (text) => .map((char) => CHARS.indexOf(char).toString().padStart(2, ‘0’)).join('');
const decode = (digits) => digits.toString().split(/(??:..)*$)/).map((idx) => CHARS).join('');
also is there a scratch version
- gosoccerboy5
-
Scratcher
1000+ posts
Node.Js encode/decode
lol you tested it on the exact same string as I didgr8 minds think alike
- Discussion Forums
- » Advanced Topics
-
» Node.Js encode/decode
?:..)*$)/).map((idx) => CHARS).join('');