How would I make a secret code on Javascript?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Calla
    New Member
    • May 2010
    • 2

    #1

    How would I make a secret code on Javascript?

    Here is what I have now as a base code. Basically, it is two text inputs, one on top of the other. In one, you enter text, and in the other, the text is changed. What I would like to do is have you enter text in one box and in the other, have it turn out like this:
    A=B
    B=C
    C=D
    D=F
    E=E
    F=H
    G=G
    H=I
    I=J
    K=K
    L=M
    M=N
    N=P
    O=O
    P=Q
    Q=S
    R=R
    S=T
    T=U
    U=V
    V=W
    W=X
    X=Y
    Y=Z
    Z=A

    So for instance, if I entered the word "hi" in the first box and clicked the submit button, in the second box, "ij" would appear.
    Here is my base code. Can anyone kindly help me?

    Code:
    <HTML>
    <HEAD>
    
    <SCRIPT LANGUAGE="JavaScript">
    <!-- Beginning of JavaScript -
    
    function encode(text) { Ref="0123456789abcdefghijklmnopqrstuvwxyz.-~ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Result=""
    for (Count=0; Count<text.length; Count++) {
    
    Char=text.substring (A=b);
    Num=Ref.indexOf (Char);
    EncodeChar=Ref.substring(Num+1, Num+2)
    Result += EncodeChar
    } document.form1.result.value=Result
    }
    // - End of JavaScript - -->
    </SCRIPT>
    
    </HEAD>
    <BODY bgcolor="beige">
    
    
    <FORM name="form1">
    <table border=0><tr><td>
    Type your word<td><INPUT NAME="input" TYPE=Text><br><td>
    <INPUT TYPE=Button VALUE="submit" onClick="encode(this.form.input.value)"><tr><td>
    Result:<td><INPUT NAME="result" TYPE=Text>
    
    </table>
    </FORM>
    </BODY>
    </HTML>
    Last edited by Calla; May 16 '10, 12:03 AM. Reason: Deleting unneeded spaces.
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    You could use "BCD...XYZA " for Ref. This would give you the correct code for each letter if you get the next letter.

    Note that you can use charAt to get a particular letter from the input.

    Comment

    • Calla
      New Member
      • May 2010
      • 2

      #3
      Originally posted by acoder
      You could use "BCD...XYZA " for Ref. This would give you the correct code for each letter if you get the next letter.

      Note that you can use charAt to get a particular letter from the input.
      Wait, so I can just replace Ref with BCD...XYZA? Also, whre do I enter the A=B, and so on?

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        I meant the rest of the letters of the alphabet there.

        You don't need to define the encoding because that's being done in your code. On line 11 you either need to use charAt or substring like you've used it on line 13

        Comment

        Working...