Click to See Complete Forum and Search --> : coverting a string into another string


totemi
09-21-2004, 02:57 PM
Hi out there
I'm trying to write some code to take a string and convert it into a "coded" string. Seemed to be going OK cept i'm supposed to b doing it using 2 functions - I'm OK if just use the function called makeCode but as soon as I use doMakeCode as well, instead of getting a string returned I get "undefined". Heres my attempted code including doMakeCode, if anyone can point me in right direction i'd b real greatful - i think i'm going mad with frustration!!!!

<HTML>
<HEAD>
<TITLE>code</TITLE>
<SCRIPT>
function makeCode(aString)
{

var codeArray = ['q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n', 'm'];

var codeString;
var output = new Array();

for (var position = 0; position < aString.length; position++)
output[output.length] = codeArray[aString.charCodeAt(position)-97]

return output.join('');
};

function doMakeCode()
{
var firstString,encodedString;
document.crypt.outString.value = '';
firstString = document.crypt.inString.value;
firstString = firstString.toLowerCase();
encodedString = makeCode(givenString);
document.crypt.outString.value = encodedString;
document.crypt.inString.focus();
};

</SCRIPT>
</HEAD>
<BODY>

<FORM NAME = "crypt">

Type message
<BR>
<INPUT TYPE = "text"
NAME = "inString"
VALUE = ''>
<BR>
<INPUT TYPE = "button"
VALUE = "Code"
ONCLICK = "document.crypt.outString.value = makeCode(document.crypt.inString.value)">
<BR>
Coded message
<BR>
<INPUT TYPE = "text"
NAME = "outString"
VALUE = ''>
<BR>

</FORM>
</BODY>
</HTML>

sciguyryan
09-21-2004, 03:01 PM
This line seems to be your problem, the variable you are entering as a perameter is not a variable that has been previously defined.

encodedString = makeCode(givenString);



RyanJ

totemi
09-21-2004, 04:22 PM
Thanks 4 reply Ryan.

Just realised I've posted 2 mistakes in the code - the line I put in forum:


encodedString = makeCode(givenString);

should say

encodedString = makeCode(firstString);


ALSO

ONCLICK = "document.crypt.outString.value = makeCode(document.crypt.inString.value)">

should say

ONCLICK = "document.crypt.outString.value = doMakeCode(document.crypt.inString.value)">

BUT it still won't work!!! - I keep getting undefined.

You don't realise how frustrating programming is til u have a go, do u!!!

Charles
09-21-2004, 08:38 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<meta name="Content-Style-Type" content="text/css">
<title>Example</title>

<style type="text/css">
<!--
div {width:15em}
fieldset {padding:1ex}
textarea {display:block; margin:auto; width:10em}
button {display:block; margin:auto}
-->
</style>

<script type="text/javascript">
<!--
enCypherArray = {a: 'q', b: 'w', c: 'e', d: 'r', e: 't', f: 'y', g: 'u', h: 'i', i: 'o', j: 'p', k: 'a' , l: 's', m: 'd', n: 'f', o: 'g', p: 'h', q: 'j', r: 'k', s: 'l', t: 'z', u: 'x', v: 'c', w: 'v', x: 'b', y: 'n', z: 'm'}

deCypherArray = new Object();
for (key in enCypherArray) {deCypherArray[enCypherArray[key]] = key};

String.prototype.enCypher = function () {return this.replace (/\w/g, function (c) {r = enCypherArray [c]; return r ? r : c})}

String.prototype.deCypher = function () {return this.replace (/\w/g, function (c) {r = deCypherArray [c]; return r ? r : c})}
// -->
</script>

</head>
<body>
<div>
<textarea id="area"></textarea>
<button onclick="this.firstChild.data = this.firstChild.data == 'encypher' ? 'decypher' : 'encypher'; document.getElementById('area').value = this.firstChild.data == 'decypher' ? document.getElementById('area').value.enCypher () : document.getElementById('area').value.deCypher ()">encypher</button>
</div>
</body>
</html>

Charles
09-22-2004, 07:28 AM
Or, perhaps...

<script type="text/javascript">
<!--
String.prototype.translate = function (from, to) {
var f = from.split('');
var t = to.split('');
var o = new Object();
for (var i = 0; i < f.length; i++) {o[f[i]] = t[i]};
return this.replace (/./g, function (c) {return o[c] || c});
}

alert ('foo, fie'.translate ('abcdefghijklmnopqrstuvwxyz', 'qwertyuiopasdfghjklzxcvbnm'))
// -->
</script>

totemi
09-22-2004, 01:36 PM
Thanks Charles i'll give it a go