JSFiddle - React, Tailwind, and code Playground

by NerfAnarchist

HTML

<label for="beginning_text">Start:</label><br />
<textarea id="beginning_text"></textarea><br /><br />

<label for="pword">Password:</label><br />
<input id="pword" type="text" /><br />

<button id="encode">Encode Text</button>
<button id="encode">Decode Text</button><br /><br />

<label for="ending_text">End:</label><br />
<textarea id="ending_text"></textarea>

CSS

textarea {
    margin: 0px;
    width: 100%;
    height: 200px;
}

input {
    margin: 0px;
    width: 100%;
}

JavaScript

var password = 'this is a test this is a test this is a test this is a t trsa',
    len = Math.floor(password.length / 5), i,
    keys = [];

// seperate the password string into 5 character divisions
for (i = 0; i < len; i = i + 1) {
    keys.push(password.substring(i * 5, (i * 5) + 5));
}

// if some of the string is leftover that does not amount to 5 then make it the last array element
if (password.substring(i * 5).length !== 0) {
    keys.push(password.substring(i * 5));
    
    // repeat the last letter until the last array element is 5 characters
    for (i = 0; i < 5; i = i + 1) {
        if (keys[len][i] === undefined) {
            keys[len] = keys[len] + keys[len][i - 1];
        }
    }
}


console.log(keys);