I am creating a encrypting program, which changes the ASCII value of the characters by 78 then outputting them as an alert. However, the alert isn't popping up.
I believe the problem lies in the second for loop where the fromCharCode() and getCharAt() are used.
Here is the HTML code
Code:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<script src="encrypt.js"></script>
<script src="decrypt.js"></script>
<title>Encrypter/Decrypter</title>
</head>
<body>
<div id="big_wrapper">
<p id="encrypt">Encrypt</p>
<input type="button" value="Click to Encrypt" name="encrypt" onClick="getTxt()">
<p id="decrypt">Decrypt</p>
</div>
</body>
</html>
Here is the Javascript code:
Code:
function getTxt(){
var x=prompt("Enter the text you want to encrypt");
alert(encrypt(x));
}
function setCharAt(str,index,chr) {
if(index > str.length-1) return str;
return str.substr(0,index) + chr + str.substr(index+1);
}
function encrypt(text){
var i =0; //preparing all of the variables
var cryptarray = new Array();
for (i=0;i<1000;i++){
cryptarray[i]=null;
}
for (i=0;i<text.length;i++){
cryptarray[i]=text.fromCharCode((text.charCodeAt(i)+78)) //loops through the string, taking characters and adding 78 to their ascii value and putting them in the temp array
}
var temptext = text;
for (i=0;i<text.length;i++){
temptext.setCharAt(i)=cryptarray[i]; //converts the array into a string
}
return temptext;
}