Quick actions

cmd+k|ctrl+k

Navigation

Languages

Encode Decode Str

Snippet info

Language

JavaScript

Visibility

public

Author

soham57

Created

2022-07-11T11:11:09.313512Z

Updated

2022-07-11T11:11:09.313512Z

function encode(arr){
    let encodedStr='';
    for(let x of arr){
        encodedStr=encodedStr.concat(x.length,x);
    }
    
    console.log(encodedStr);
    return encodedStr;
}

function decode(str){
    const arr=[];
    let wordLength=str[0];
    for(let x=1; x<str.length; x++){
        let word=''
        while(wordLength--){
            word+=str[x++];
        }
        arr.push(word);
        
        wordLength=str[x]
    }
    console.log(arr)
    return arr;
}
decode(encode(["p","oil","p9095o","123455"]));
INFO