Click to See Complete Forum and Search --> : Looping and Coding
S1L3NC3
10-05-2003, 05:16 PM
Alright heres the deal, any brave enough to assist me here (and not criticize too awful much) would be greatly appreciated.
I am in the process of making a very, very simple text encoder/decoder. I am fairly new to javascript and dont have a great knowledge of commands and syntax yet.
I dont want an entire source for what i want to do, because i am doing this to learn as i go, and as you can imagine, its not really easy.
Heres what i have so far as the heart of the encoding script:
function doit()
{
var a,b,c,d,x,y,z,i;
x=document.ioboxes.input.value;
<!-- SET i AND CREATE Y ARRAY -->
i=x.length;
y=x.split("")
<!-- ENCODE STRING -->
while (i!=0)
{
b = y[i].charCodeAt();
b= b+6*3;
c=String.fromCharCode(b);
i=i-1;
}
<!-- FINAL OUTPUT -->
document.ioboxes.output.value=c;
}
The problem is, i know how to increment it to go through that loop enough times to cover the whole string, but i dont know how to switch it to the next char every time it loops. the way it is written now, itll just keep encoding the same Character, the first one.
Thanks so so much in advance.
Charles
10-05-2003, 05:21 PM
Ok, let's start from the start. In English, and with no references to the flow of the program, what exactly are you trying to do? Obviously you are trying to set up some kind of a code, but how does the code work?
Charles
10-05-2003, 05:35 PM
I think, however, that this may be what you are after...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<style type="text/css">
<!--
label {display:block; margin:1em 0em}
input {display:block}
-->
</style>
<form action="">
<div>
<label>Text<input type="text" onchange="encoded.value = this.value.replace(/./g, function (c) {return String.fromCharCode(c.charCodeAt(0)+6*3)})"></label>
<label>Encoded<input type="text" name="encoded" readonly></label>
</div>
</form>
S1L3NC3
10-05-2003, 05:38 PM
Yes, but i dont want it given to me, im doing this to learn javscript better.
I also want a decoder.
Heres the complete source of what i have so far.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script language="JavaScript" type="text/javascript">
function doit()
{
var a,b,c,d,x,y,z,i;
x=document.ioboxes.input.value;
<!-- SET i AND CREATE Y ARRAY -->
i=x.length;
y=x.split("")
<!-- ENCODE STRING -->
while (i!=0)
{
b = y[i].charCodeAt();
b= b+6*3;
c=String.fromCharCode(b);
i=i-1;
}
<!-- FINAL OUTPUT -->
document.ioboxes.output.value=c;
}
</script>
</head>
<body bgcolor="gray" text="white">
<div align="center">
<BR>
<p>Encoder/Decoder</p>
<form name="ioboxes">
<textarea cols="50" rows="8" name="input"></textarea>
<BR><BR>
<input type="button" value="Encode" name="button1" onClick="doit()">
<input type="button" value="Decode" name="button2" onClick="undoit()">
<BR><BR>
<textarea readonly cols="50" rows="8" name="output"></textarea>
</form>
</body>
</html>
Quite possible the messiest and most un-robust source youll ever see, but hey, im learning ;)
The code itself is just going to take the ASCII value of each character and add 6 to it and multiply that number by three.
Then convert the ASCII total back to the corresponding character.
Charles
10-05-2003, 06:30 PM
A couple of things...
1) The only way that I can help you is to show you how it is done.
2) Each line and each variable name in JavaScript comes with a huge overhead. It's important to conserve.
3) b+6*3 is the same thing as b+18. You want (b+6)*3.
4) You cannot learn JavaScript by playing around with it. You need a systematic course of study. Read, several times and cover to cover, JavaScript : The Definitive Guide by David Flanagan.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<style type="text/css">
<!--
textarea {display:block}
-->
</style>
<script type="text/javascript">
<!--
String.prototype.encode = function () {return this.replace(/./g, function (c) {return String.fromCharCode((c.charCodeAt(0)+6)*3)})};
String.prototype.unEncode = function () {return this.replace(/./g, function (c) {return String.fromCharCode(c.charCodeAt(0)/3-6)})};
// Or, if you want to step through each character yourself then use these two methods
String.prototype.encode = function () {var a = this.split(''); for (var i=0; i<a.length; i++) {a[i] = String.fromCharCode((a[i].charCodeAt(0)+6)*3)}; return a.join('')}
String.prototype.unEncode= function () {var a = this.split(''); for (var i=0; i<a.length; i++) {a[i] = String.fromCharCode(a[i].charCodeAt(0)/3-6)}; return a.join('')}
// -->
</script>
<form action="">
<div>
<textarea name="text" cols="50" rows="8"></textarea>
<button onclick="this.form.text.value = this.form.text.value.encode()">Encode</button>
<button onclick="this.form.text.value = this.form.text.value.unEncode()">Un Encode</button>
</div>
</form>
S1L3NC3
10-05-2003, 10:38 PM
Thats funny, i leanred HTML that way.
And FYI, i have that book, and am in the process of reading it.
But in due respect, thank you for your efforts once again Sir Charles.