Discuss Scratch

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:

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

Raihan142857 wrote:

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('');
huh, that doesn't work with spaces.
decode(encode("hello world")); //helloworld
unless that's on purpose, a simple fix would probably be to add a space character to the CHARS variable.
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

gosoccerboy5 wrote:

Raihan142857 wrote:

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('');
huh, that doesn't work with spaces.
decode(encode("hello world")); //helloworld
unless that's on purpose, a simple fix would probably be to add a space character to the CHARS variable.
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('');
oops, thought I fixed that
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

gosoccerboy5 wrote:

Raihan142857 wrote:

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('');
huh, that doesn't work with spaces.
decode(encode("hello world")); //helloworld
unless that's on purpose, a simple fix would probably be to add a space character to the CHARS variable.
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('');
idk why but
Chiroyce
Scratcher
1000+ posts

Node.Js encode/decode

pokeshah wrote:

idk why but

gosoccerboy5 wrote:

a simple fix would probably be to add a space character to the CHARS variable.
pokeshah
Scratcher
100+ posts

Node.Js encode/decode

rdremia wrote:

Chiroyce wrote:

pokeshah wrote:

idk why but

gosoccerboy5 wrote:

a simple fix would probably be to add a space character to the CHARS variable.
it is
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')))
mhmmmm
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

Raihan142857 wrote:

const CHARS = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`1234567890-=~!@#$%^&*()_+\;\',./{}|:"<>?';
const encode = (text) => .map((char) => CHARS.indexOf(char).toString().padStart(2, ‘0’)).join('');
const decode = (digits) => digits.toString().split(/(??:..)*$)/).map((idx) => CHARS).join('');
Thank you!
also is there a scratch version
Raihan142857
Scratcher
1000+ posts

Node.Js encode/decode

pokeshah wrote:

Raihan142857 wrote:

const CHARS = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`1234567890-=~!@#$%^&*()_+\;\',./{}|:"<>?';
const encode = (text) => .map((char) => CHARS.indexOf(char).toString().padStart(2, ‘0’)).join('');
const decode = (digits) => digits.toString().split(/(??:..)*$)/).map((idx) => CHARS).join('');
Thank you!
also is there a scratch version
literally just search “encode” and there'll be a million results
gosoccerboy5
Scratcher
1000+ posts

Node.Js encode/decode

Raihan142857 wrote:

lol you tested it on the exact same string as I did
gr8 minds think alike

Powered by DjangoBB