I have an old project with legacy code that I cannot get to run now

I made a simple desktop app sometime back that is meant to be my own custom plaintext encryption software(yes, I know, a weird project when I was bored). Now I actually need the software working as I see a need to encrypt some of my text based writeups on my laptop. I use ElectronJS framework for my desktop apps.

The problem i’ve spotted looking at the code is that the javascript charAt(); method is not accepting my for loop counter as a valid return value and that’s what I need the code to do. I used alert() to debug this.
Long story short, here’s the JS code:

<script>

//Function to search and sort plaintext to ciphertext

//const fs = require('fs'); //requiring the filesystem module from nodejs to help us handle files
var plaintext;
var cipherstring = new Array(500);
var newstring;

document.getElementById("Encryptbutton").onclick = () => {
	//first we save the plaintext file
	var plaintext = document.getElementById("textbox").value; //store value of textbox in plaintext
	//fs.writeFile ('text.txt',data, (err) => {
		//if (err) throw err;
		//}) //this will handle any error writing the file by ignoring it
		
	//plaintext = data; //store the value of the textbox in variable called plaintext
	
	var i;
	for (i=0;i=plaintext.length;i++){ //loop to run through plaintext
	
	var x = plaintext.charAt(plaintext.length); //x will hold the positions of characters in the array and return their value.
	//alert(i); //testing to see what i returns at this point
	//alert(x); //testing to see what x returns at this point
	switch (x) { //simple way of testing each character in the array of strings
		case "a" || "A":
		cipherstring.push("N[↙],N[β†˜],E[β†’]W");
		break;
		case "b" || "B":
		cipherstring.push("N[↓]S,N[β†·]O,O[β†·]S");
		break;
		case  "c" || "C":
		cipherstring.push("N[β†Ά]S");
		break;
		case "d" || "D":
		cipherstring.push("N[↓]S,N[β†·]S");
		break;
		case "e" || "E":
		cipherstring.push("E[]");
		break;
		case "f" || "F":
		cipherstring.push("N[↓]S,N[β†’],W[β†’]O");
		break;
		case "g" || "G":
		cipherstring.push("N[β†Ά]S,S[β†’]O");
		break;
		case "h" || "H":
		cipherstring.push("1N[↓]S,E[β†’]W,3N[↓]S");
		break;
		case  "i" || "I":
		cipherstring.push("N[↓]S");
		break;
		case "j" || "J":
		cipherstring.push("N[β†’],N[↓]S,S[←]");
		break;
		case "k" || "K":
		cipherstring.push("N[↓]S,O[β†—],O[β†˜]");
		break;
		case "l" || "L":
		cipherstring.push("N[↓]S,S[β†’]");
		break;
		case "m" || "M":
		cipherstring.push("N[↓]S,N[β†’]O,3N[β†’]O,3N[↓]S");
		break;
		case "n" || "N":
		cipherstring.push("N[]");
		break;
		case "o" || "O":
		cipherstring.push("O[]");
		break;
		case "p" || "P":
		cipherstring.push("N[↓]S,N[β†·]O");
		break;
		case "q" || "Q":
		cipherstring.push("N[β†·]S,S[β†·]N,O[β†˜]");
		break;
		case "r" ||  "R":
		cipherstring.push("N[↓]S,N[β†·]O,O[β†˜]");
		break;
		case "s" || "S":
		cipherstring.push("S[]");
		break;
		case "t" || "T":
		cipherstring.push("N[β†’],N[↓]S");
		break;
		case "u" || "U":
		cipherstring.push("N[β†Ά]S,S[β†Ά]3N");
		break;
		case "v" || "V":
		cipherstring.push("1N[↓]S,S[↑]3N");
		break;
		case "w" || "W":
		cipherstring.push("W[]");
		break;
		case "x" || "X":
		cipherstring.push("1N[↓]3S,1S[↑]3N");
		break;
		case "y" || "Y":
		cipherstring.push("1N[↓]O,S[β†’]O,O[β†—]");
		break;
		case "z" || "Z":
		cipherstring.push("N[β†’],3N[β†’]S,1S[β†’]");
		case "0":
		cipherstring.push(".0");
		break;
		case "1":
		cipherstring.push(".1");
		break;
		case "2":
		cipherstring.push(".2");
		break;
		case "3":
		cipherstring.push(".3");
		break;
		case "4":
		cipherstring.push(".4");
		break;
		case "5":
		cipherstring.push(".5");
		break;
		case "6":
		cipherstring.push(".6");
		break;
		case "7":
		cipherstring.push(".7");
		break;
		case "8":
		cipherstring.push(".8");
		break;
		case "9":
		cipherstring.push(".9");
		break;
		default:
		cipherstring.push("_symbol_");
	} //endswitch
	
	newstring = ":" + cipherstring[i];
	
	} //endforloop
	
	//Finally,we want to display and save the encrypted plaintext as ciphertext
	
	document.getElementById("textArea").value= newstring;
	//to call on external javascript files in electron, we use the format;
	require('./savefunction.js') //calling my function to save to run here...
}
</script>

Thanks in advance for any help or suggestions on how to rewrite the code.
Regards,
Fejiro

Hi Fejiro - welcome to the forums :slight_smile:

The problem seems to be here:

Should you be using i instead of plaintext.length for the charAt method? That will ensure you go through the each letter with each loop iteration and return the converted version.

Let me know if that works?

Cheers,
Kirupa :grinning:

Hi, It was var x = plaintext.charAt(i); initially before I uploaded my source here, I must have done one last debug attempt before I posted here :slight_smile: All thesame, it still does’nt seem to work.

This is hard to run locally, so I am just going by what I see in the code. Does the for loop run all the way through on your setup?

Yes I believe so, as tested with the two alert(); functions when I was debugging, it loops between them continuously, the first i.e alert(i); correctly alerting the length of plaintext from the textbox and the second i.e alert(x); returning nothing/empty.

I think you can’t run it locally from your end because a browser may not have require(); function. Like I said earlier, I’m using electronJS and NodeJS framework.

Perhaps I should attach screenshots?

The challenge is that what you posted here is (understandably) different than your node/electron environment. If you can post a working repro fully in a browser environment like codepen.io or replit, that would make it much easier.

Your code looks good to me (though, I should mention I easily miss some basic things :joy:), so there is something subtle going on that running it through in the browser would help greatly.

Understandable, I had to a friend help me today, I did a walkthrough of the code with him and he helped me fix it. here’s the simplified code now that works on browser(he does’nt use electronJS either :slight_smile: )

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form>
        <input type="text" id="user-text">
        <button id="encrypt-btn">Encrypt</button>
    </form>
    <h2 class="result" id="result"></h2>
    <script src="main.js"></script>
</body>
</html>

Main.js source:

const userInput = document.getElementById("user-text");
const encryptBtn = document.getElementById("encrypt-btn");
const result = document.getElementById("result");

let plaintext;
const cipherstring = [];
let newString ="";

encryptBtn.addEventListener("click", (e) => {
    e.preventDefault();
    plaintext = userInput.value;
    for (let i = 0; i < plaintext.length; i++) {
        let x = plaintext.charAt(i);
        switch (x) { //simple way of testing each character in the array of strings
            case "a" || "A":
            cipherstring.push("N[↙],N[β†˜],E[β†’]W");
            break;
            case "b" || "B":
            cipherstring.push("N[↓]S,N[β†·]O,O[β†·]S");
            break;
            case  "c" || "C":
            cipherstring.push("N[β†Ά]S");
            break;
            case "d" || "D":
            cipherstring.push("N[↓]S,N[β†·]S");
            break;
            case "e" || "E":
            cipherstring.push("E[]");
            break;
            case "f" || "F":
            cipherstring.push("N[↓]S,N[β†’],W[β†’]O");
            break;
            case "g" || "G":
            cipherstring.push("N[β†Ά]S,S[β†’]O");
            break;
            case "h" || "H":
            cipherstring.push("1N[↓]S,E[β†’]W,3N[↓]S");
            break;
            case  "i" || "I":
            cipherstring.push("N[↓]S");
            break;
            case "j" || "J":
            cipherstring.push("N[β†’],N[↓]S,S[←]");
            break;
            case "k" || "K":
            cipherstring.push("N[↓]S,O[β†—],O[β†˜]");
            break;
            case "l" || "L":
            cipherstring.push("N[↓]S,S[β†’]");
            break;
            case "m" || "M":
            cipherstring.push("N[↓]S,N[β†’]O,3N[β†’]O,3N[↓]S");
            break;
            case "n" || "N":
            cipherstring.push("N[]");
            break;
            case "o" || "O":
            cipherstring.push("O[]");
            break;
            case "p" || "P":
            cipherstring.push("N[↓]S,N[β†·]O");
            break;
            case "q" || "Q":
            cipherstring.push("N[β†·]S,S[β†·]N,O[β†˜]");
            break;
            case "r" ||  "R":
            cipherstring.push("N[↓]S,N[β†·]O,O[β†˜]");
            break;
            case "s" || "S":
            cipherstring.push("S[]");
            break;
            case "t" || "T":
            cipherstring.push("N[β†’],N[↓]S");
            break;
            case "u" || "U":
            cipherstring.push("N[β†Ά]S,S[β†Ά]3N");
            break;
            case "v" || "V":
            cipherstring.push("1N[↓]S,S[↑]3N");
            break;
            case "w" || "W":
            cipherstring.push("W[]");
            break;
            case "x" || "X":
            cipherstring.push("1N[↓]3S,1S[↑]3N");
            break;
            case "y" || "Y":
            cipherstring.push("1N[↓]O,S[β†’]O,O[β†—]");
            break;
            case "z" || "Z":
            cipherstring.push("N[β†’],3N[β†’]S,1S[β†’]");
            case "0":
            cipherstring.push(".0");
            break;
            case "1":
            cipherstring.push(".1");
            break;
            case "2":
            cipherstring.push(".2");
            break;
            case "3":
            cipherstring.push(".3");
            break;
            case "4":
            cipherstring.push(".4");
            break;
            case "5":
            cipherstring.push(".5");
            break;
            case "6":
            cipherstring.push(".6");
            break;
            case "7":
            cipherstring.push(".7");
            break;
            case "8":
            cipherstring.push(".8");
            break;
            case "9":
            cipherstring.push(".9");
            break;
            default:
            cipherstring.push("_symbol_");
        } //endswitch

	newString += ":" + cipherstring[i];
    result.innerText = newString;
    }
})

Thanks for the support Kirupa, and hey, don’t try cracking my ciphertext lol :shushing_face: :grin:

Haha! Glad you got it sorted out :slight_smile:

If you are up for another cipher-related challenge, you may find this interesting:

1 Like