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

**URL:** <https://forum.kirupa.com/t/i-have-an-old-project-with-legacy-code-that-i-cannot-get-to-run-now/649806>\
**Category:** programming\
**Created:** [November 27, 2021, 8:55pm UTC](https://forum.kirupa.com/t/i-have-an-old-project-with-legacy-code-that-i-cannot-get-to-run-now/649806 "2021-11-27T20:55:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fejiro\_Eruotor](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/fejiro_eruotor/32/13483_2.png) [@Fejiro\_Eruotor](https://forum.kirupa.com/u/Fejiro_Eruotor)\
**Post date:** [November 27, 2021, 8:55pm UTC](https://forum.kirupa.com/t/i-have-an-old-project-with-legacy-code-that-i-cannot-get-to-run-now/649806/1 "2021-11-27T20:55:53Z")

</div>

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:

```auto
<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

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [November 27, 2021, 10:51pm UTC](https://forum.kirupa.com/t/i-have-an-old-project-with-legacy-code-that-i-cannot-get-to-run-now/649806/2 "2021-11-27T22:51:33Z")

</div>

Hi Fejiro - welcome to the forums 🙂

The problem seems to be here:

> [@Fejiro\_Eruotor](#):
>
> `var x = plaintext.charAt(plaintext.length);`

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 😀

---

<div class="post-metadata">

**Author:** ![Fejiro\_Eruotor](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/fejiro_eruotor/32/13483_2.png) [@Fejiro\_Eruotor](https://forum.kirupa.com/u/Fejiro_Eruotor)\
**Post date:** [November 27, 2021, 10:56pm UTC](https://forum.kirupa.com/t/i-have-an-old-project-with-legacy-code-that-i-cannot-get-to-run-now/649806/3 "2021-11-27T22:56:00Z")

</div>

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 🙂 All thesame, it still does’nt seem to work.

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [November 28, 2021, 6:45am UTC](https://forum.kirupa.com/t/i-have-an-old-project-with-legacy-code-that-i-cannot-get-to-run-now/649806/4 "2021-11-28T06:45:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Fejiro\_Eruotor](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/fejiro_eruotor/32/13483_2.png) [@Fejiro\_Eruotor](https://forum.kirupa.com/u/Fejiro_Eruotor)\
**Post date:** [November 28, 2021, 8:07am UTC](https://forum.kirupa.com/t/i-have-an-old-project-with-legacy-code-that-i-cannot-get-to-run-now/649806/5 "2021-11-28T08:07:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Fejiro\_Eruotor](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/fejiro_eruotor/32/13483_2.png) [@Fejiro\_Eruotor](https://forum.kirupa.com/u/Fejiro_Eruotor)\
**Post date:** [November 28, 2021, 8:14am UTC](https://forum.kirupa.com/t/i-have-an-old-project-with-legacy-code-that-i-cannot-get-to-run-now/649806/6 "2021-11-28T08:14:40Z")

</div>

Perhaps I should attach screenshots?

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [November 28, 2021, 7:23pm UTC](https://forum.kirupa.com/t/i-have-an-old-project-with-legacy-code-that-i-cannot-get-to-run-now/649806/7 "2021-11-28T19:23:10Z")

</div>

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](http://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 😂), so there is something subtle going on that running it through in the browser would help greatly.

---

<div class="post-metadata">

**Author:** ![Fejiro\_Eruotor](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/fejiro_eruotor/32/13483_2.png) [@Fejiro\_Eruotor](https://forum.kirupa.com/u/Fejiro_Eruotor)\
**Post date:** [November 28, 2021, 7:57pm UTC](https://forum.kirupa.com/t/i-have-an-old-project-with-legacy-code-that-i-cannot-get-to-run-now/649806/8 "2021-11-28T19:57:45Z")

</div>

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 🙂 )

HTML:

```auto
<!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:

```auto
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 🤫 😁

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [November 28, 2021, 9:42pm UTC](https://forum.kirupa.com/t/i-have-an-old-project-with-legacy-code-that-i-cannot-get-to-run-now/649806/9 "2021-11-28T21:42:21Z")

</div>

Haha! Glad you got it sorted out 🙂

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

> **[Build a Caesar's Cipher](https://www.kirupa.com/html5/caesar_cipher.htm)**
>
> Get a deep dive into building a fully functioning app using HTML, CSS, and JS by creating this fun encryption app.
