javascript hints respectfully required.

Discuss Programming

javascript hints respectfully required.

Postby Calum » Tue Sep 21, 2004 3:36 pm

hello. it's for university, so it has to be my own work. this means i'd appreciate it if you are going to help me that you try and be as minimalist as possible, and make me figure it out myself if poss. i am pretty dumb sometimes though.

basically i am trying to write an html page with javascript init that will display a text box and a button and another text box. you type something in the first text box, then click the button and the letters are all swapped with their correspondingly numbered letters from an array (a is the first letter in the alphabet, so if s is the first value in the array (at index 0), a gets output as s), with upper case characters getting treated as lower case and spaces getting output as spaces.

i am supposed to make use of the unicode values of words by using charCodeAt() but i can't basically make it work. the code is below, the function doSubEncipher() and the first bit of subEncipher() were written already and i have put the rest in myself. The GUI form works but the thing won't output anything no matter what i do.

as i say, don't tell me how to do it because it's supposed to be my own work, but if you happen to bsee where i have gone wrong, then hints or criticisms wouldn't go amiss.

thank you etc in advance!

Code: Select all
<HTML>

<HEAD>

<SCRIPT>

function subEncipher(aString)
{
var subArray = ['s','c','h','n','i','t','z','e','l','m','o','p','q','r','u','v','w','x','y','a','b','d','f','g','j','k'];
var outString = "";
var codeString = "";

for(var count = 0; count = aString.length; count = count + 1)
{
if (aString.charAt(count) = " ")
   {
      outString = outString + " ";
   }
else
   {
   codeString = (aString.charCodeAt(count) - 97);
   outString = outString + subArray[codeString];

   };
return outString
}

function doSubEncipher()
{
   var givenString,encodedString;
   document.encipher.outputString.value ='';
   givenString = document.encipher.inputString.value;
   givenString = givenString.toLowerCase();// note that toLowerCase leaves non-alpha characters unchanged
   encodedString = subEncipher(givenString);
   document.encipher.outputString.value = encodedString;
   document.encipher.inputString.focus();// reset focus
}

</SCRIPT>

</HEAD>

<BODY>

<form NAME = "encipher">
Enter String<br>
<INPUT  TYPE = "text"
        NAME = "inputString"
        VALUE = ''>
<br>
<input TYPE = "button"
        VALUE = "Encipher"
        ONCLICK = "document.encipher.outputString.value = doSubEncipher()">
<br>
<INPUT  TYPE = "text"
        NAME = "outputString"
        VALUE = ''>

</BODY>

</HTML>

User avatar
Calum
guru
guru
 
Posts: 1343
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland

Postby Void Main » Tue Sep 21, 2004 8:44 pm

You are actually very close. Mostly syntax errors and a missing brace. Here's a working model in case you give up:

http://voidmain.is-a-geek.net/files/mis ... cript.html

There are a few major things that I can think of. First I like to indent a little differently than you. Proper indention will cause you to see the missing curly braces. You might also use syntax highlighting editor (VIM, etc) which also helps to spot missing or broken statements. Also, looking at your JavaScript console in mozilla will show errors. For instance you had an invalid lvalue on a specific line. When I looked at the line I saw that it was a conditional statement (if) and you had an "=" where you meant to have an "==". Couple of other minor catches and I got it working. I'm not a JavaScript programmer or fan so it's entirely possible that something is not right, but it does work now.
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby Calum » Wed Sep 22, 2004 4:05 am

void main, i nreally appreciate your comments a lot! i am going to take all of your advice, although i don't think i can look at your working model until i am properly finished with my own one! now i think of it i don't know why i wasn't using vim for this, i usually do for html.

thanks again!
User avatar
Calum
guru
guru
 
Posts: 1343
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland

Postby X11 » Wed Sep 22, 2004 6:35 am

I use vim for everything... probably why my personal hygene can get so bad.
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Postby Void Main » Wed Sep 22, 2004 8:51 am

X11 wrote:I use vim for everything... probably why my personal hygene can get so bad.


Ewwww. Yeah, it doesn't make good soap.

I also just noticed a missing "</form>" tag. Fixed.
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby X11 » Wed Sep 22, 2004 11:02 am

Yea I try to hack into the dirt on my skin with shell scripts written in vim.

so far unsucsessful =/
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Postby Calum » Mon Sep 27, 2004 3:11 am

in this country (and by the looks of this picture, some others too) vim is cream cleaner for cookers, worktops etc. see what consumers have to say about it here.

Image

I don't think it would go too well in soup, no. It is probably enough to get the dirt off of X11 though...
User avatar
Calum
guru
guru
 
Posts: 1343
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland

Postby Void Main » Mon Sep 27, 2004 8:44 am

Well I'll be. Maybe VIM *can* get the dirt off X11. :)
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron