/    Sign up×
Community /Pin to ProfileBookmark

coverting a string into another string

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>

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@sciguyryanSep 21.2004 — 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
Copy linkTweet thisAlerts:
@totemiauthorSep 21.2004 — 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!!!
Copy linkTweet thisAlerts:
@CharlesSep 22.2004 — [font=monospace]<!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>[/font]
Copy linkTweet thisAlerts:
@CharlesSep 22.2004 — Or, perhaps...

[font=monospace]<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>[/font]
Copy linkTweet thisAlerts:
@totemiauthorSep 22.2004 — Thanks Charles i'll give it a go
×

Success!

Help @totemi spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...