Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11

Thread: Debugging help

  1. #1
    Regular Coder
    Join Date
    Oct 2011
    Posts
    106
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Debugging help

    I've been working on that for days, and can't find the problem. Could one of you help? I wrote a low-level digit-shift cipher, but the script doesn't work.
    Code:
    <head>
    <script type="text/javascript">
    
    	function ciphertext()
    		{
    		var t = document.getElementById.('t')
    		t = t.toUpperCase()
    		var n = 0
    		var o = ""
    		if(n=0)
    			{
    			o=o+t
    			n=n+1
    			}
    		if(n=1)
    			{
    			if(t=="A")
    				o=o+"B"
    			if(t=="B")
    				o=o+"C"
    			if(t=="C")
    				o=o+"D"
    			if(t=="D")
    				o=o+"E"
    			if(t=="F")
    				o=o+"G"
    			if(t=="G")
    				o=o+"H"
    			if(t=="H")
    				o=o+"I"
    			if(t=="I")
    				o=o+"J"
    			if(t=="J")
    				o=o+"K"
    			if(t=="K")
    				o=o+"L"
    			if(t=="L")
    				o=o+"M"
    			if(t=="M")
    				o=o+"N"
    			if(t=="N")
    				o=o+"O"
    			if(t=="O")
    				o=o+"P"
    			if(t=="P")
    				o=o+"Q"
    			if(t=="Q")
    				o=o+"R"
    			if(t=="R")
    				o=o+"S"
    			if(t=="S")
    				o=o+"T"
    			if(t=="T")
    				o=o+"U"
    			if(t=="U")
    				o=o+"V"
    			if(t=="V")
    				o=o+"W"
    			if(t=="W")
    				o=o+"X"
    			if(t=="X")
    				o=o+"Y"
    			if(t=="Y")
    				o=o+"Z"
    			if(t=="Z")
    				o=o+"A"
    			else
    				o=o+t
    				n=n-1
    			}
    			document.getElementById('op').innerHTML = o.value
    		}
    	</script>
    </head>
    <body>
    		<b id="op">This is your result</b>
    	<input type="text" id='t' />
    	<button onclick="ciphertext()">Submit</button>
    
    </body>
    I've been working on that for days, and can't find the problem. Could one of you help? (Pardon me if I am being blunt, but why doesn't this work?)

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Well, for starters, you are using = where you need to use ==
    Code:
    		if(n=0)
    		if(n=1)
    When you use = like that, you end up actually coding the equivalent of this:
    Code:
         n = 0; if ( n != 0 )
        ...
         n = 1; if ( n != 0 )
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Then this is giving you an error:
    Code:
        var t = document.getElementById.('t')
    You have a bogus extra period there before the (

    Then, on top of that, you are only getting a reference to the field, not its value. So:
    Code:
    var t = document.getElementById('t').value;
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Is this what you were after???
    Code:
    <head>
    <script type="text/javascript">
    function ciphertext()
    {
        var txt = document.getElementById('t').value.toUpperCase();
        var o = "";
        for ( var n = 0; n < txt.length; ++n )
        {
            var ch = txt.charAt(n);
            if ( ch == "Z" ) o += "A";
            else if ( ch < "A" || ch > "Z" ) o += ch;
                 else o += String.fromCharCode( txt.charCodeAt(n) + 1 );
        }
        document.getElementById('op').innerHTML = o;
    }
    </script>
    </head>
    <body>
        <b id="op">This is your result</b><br/>
        <input type="text" id='t' />
        <button onclick="ciphertext()">Submit</button>
    
    </body>
    Last edited by Old Pedant; 10-12-2011 at 04:05 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Regular Coder
    Join Date
    Oct 2011
    Posts
    106
    Thanks
    12
    Thanked 0 Times in 0 Posts
    For starters, thanks for answering. However, it only works in Chrome, and even then it only returns undefined. What you posted doesn't work in IE, and chrome can't find it. And the reason that I didn't do that is because it is easier to program to follow your train of thought, and I think in IF, ELSE, WHILE, and (in PHP) SWITCH. I have never been good at FOR.

    In essence, you solved a problem, but uncovered another. The current code is:

    Code:
    <head>
    <script type="text/javascript">
    
    	function ciphertext()
    		{
    		var t = document.getElementById('t').value
    		t = t.toUpperCase()
    		var n = 0
    		var o = ""
    		if(n==0)
    			{
    			o=o+t
    			n=n+1
    			}
    		if(n==1)
    			{
    			if(t=="A")
    				o=o+"B"
    			if(t=="B")
    				o=o+"C"
    			if(t=="C")
    				o=o+"D"
    			if(t=="D")
    				o=o+"E"
    			if(t=="F")
    				o=o+"G"
    			if(t=="G")
    				o=o+"H"
    			if(t=="H")
    				o=o+"I"
    			if(t=="I")
    				o=o+"J"
    			if(t=="J")
    				o=o+"K"
    			if(t=="K")
    				o=o+"L"
    			if(t=="L")
    				o=o+"M"
    			if(t=="M")
    				o=o+"N"
    			if(t=="N")
    				o=o+"O"
    			if(t=="O")
    				o=o+"P"
    			if(t=="P")
    				o=o+"Q"
    			if(t=="Q")
    				o=o+"R"
    			if(t=="R")
    				o=o+"S"
    			if(t=="S")
    				o=o+"T"
    			if(t=="T")
    				o=o+"U"
    			if(t=="U")
    				o=o+"V"
    			if(t=="V")
    				o=o+"W"
    			if(t=="W")
    				o=o+"X"
    			if(t=="X")
    				o=o+"Y"
    			if(t=="Y")
    				o=o+"Z"
    			if(t=="Z")
    				o=o+"A"
    			else
    				o=o+t
    				n=n-1
    			}
    			document.getElementById('op').innerHTML = o.value
    		}
    	</script>
    </head>
    <body>
    		<b id="op">This is your result</b>
    	<input type="text" id='t' />
    	<button onclick="ciphertext()">Submit</button>
    
    </body>

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    thanks for answering. However, it only works in Chrome
    Gee. that's funny. I wonder why it (a) works form me in Chrome, (b) works for me in Firefox, (c) works for me in MSIE7 [yes, even old 7!], and (d) gives identical results in all three.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Let's examine YOUR logic.

    Let's assume that the user types in ONLY the letter "A" in your <input id="t"> field.

    So here is what happens:
    Code:
    function ciphertext()
    {
    	var t = document.getElementById('t').value
    // at this point, then t refers to a string that contains only "A"
    
    	var n = 0
    	var o = ""
    
    // when we get here, n was initialized to zero, so we will *ALWAYS* find that n==0 is true.
    // always
    	if(n==0)
    	{
    // so we *WILL* execute these next lines
    		o=o+t
    // o started as blank.  We just appended "A" to it (the value of t)
    // so now o contains "A"
    		n=n+1
    // and since n was zero, it is now 1
    	}
    
    // since n will *ALWAYS* be equal to 1 when we get here, n==1 will always be true
    	if(n==1)
    	{
    // so we ALWAYS execute the lines in this block
    		if(t=="A")
    // we said that t contained only "A", so t=="A" is true
    			o=o+"B"
    // so o already contained "A" and after appending "B" o contains "AB"
                    ...
    	}
    	document.getElementById('op').innerHTML = o.value
    // and so now the <span> with id="op" will contain "AB"
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Let's examine YOUR logic, again.

    Let's assume that the user types in "AB" in your <input id="t"> field.

    So here is what happens:
    Code:
    function ciphertext()
    {
    	var t = document.getElementById('t').value
    // at this point, then t refers to a string that contains "AB"
    
    	var n = 0
    	var o = ""
    
    // when we get here, n was initialized to zero, so we will *ALWAYS* find that n==0 is true.
    // always
    	if(n==0)
    	{
    // so we *WILL* execute these next lines
    		o=o+t
    // o started as blank.  We just appended "AB" to it (the value of t)
    // so now o contains "AB"
    		n=n+1
    // and since n was zero, it is now 1
    	}
    
    // since n will *ALWAYS* be equal to 1 when we get here, n==1 will always be true
    	if(n==1)
    	{
    // *NONE* of your tests for t=="A" or t=="B" or ... t=="Z" will be true
    // because of course now t contains *two* caharacters
    		if(t=="A")
    // so you will never get to the single lines like this:
    			   o=o+"B"
                    ...
    // instead, you will get here:
      
    	else
    		o=o+t
    // so o already contained "AB".  Appending the "AB" that is in t will make o have a value of "ABAB"
    		n=n-1
    // and no idea why you want to subtract 1 from n, which sets it back to zero...but you do
    	}
    	document.getElementById('op').innerHTML = o.value
    // and so now the <span> with id="op" will contain "ABAB"
    }
    *THIS* is the behavior you are trying to create??? Really?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Regular Coder
    Join Date
    Oct 2011
    Posts
    106
    Thanks
    12
    Thanked 0 Times in 0 Posts

    No need to insult

    I failed to place (or know) the JS equivalent of C's getchar(). I therefore (inadvertently) made it change the whole string at once. What I was trying to make happen is AB turn into AC, Hello turn into HFLMO, etc. If you could provide the code for it to call ciphertext() for each character individually (i.e., the JS equivalent of getchar()), it would be greatly appreciated. In the future, I will not react as kindly to users who act as you have. You pointed out a flaw in my script, but your diction heavily connoted condescension.

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    The "condescension" was more me reacting to your "it only works in Chrome", which I took exception to. (Especially since I had written and tested it in MSIE and FF, and knew it worked in them.)

    So then, yes, I tried to show you why your code is illogical. Perhaps with a bit of attitude.

    Anyway... There is no JavaScript equivalent of getchar(). JavaScript doesn't allow you to ask for a single character of keyboard input at a time.

    Now, that's not to say there aren't workarounds. You could, for example, use an onkeydown event on the <body> element to capture keystrokes anywhere on the page. But it's a pretty clumsy mechanism. At a minimum, if you really need to capture keystrokes, I'd suggest capturing them in an <input type="text"> field or perhaps a <textarea>. You'd still need to use an onkeydown event handler, but at least you wouldn't be interfering with the normal operation of keystrokes all over the page. You'd only capture them when the focus is on the input field.

    Is that what you really need to do? Capture keystrokes one at a time and process them *BEFORE* they get displayed to the user? That is, when the user types "A" you cause JavaScript to echo it back as "B" in the input field? It can be done, it's just a pain to do. Because you have to write code that is browser-aware (MSIE is different than all the rest, of course). It's much easier to capture the entire input field as a string and then process the full string, as then there aren't any browser dependencies.

    Oh...and on top of that, if you capture the individual keystrokes, you have to worry about the user hitting a backspace or a cursor key (for example) and figuring out how you want them to affect your on-the-fly encoding.

    So before I go to the trouble of showing you key-at-a-time, wouldn't line at a time (or, rather, <input> field at a time) be adequate?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Oh, w.t.h.

    Here. As I said, if you want to do it character at a time, you have to handle backspace and cursor keys, etc. This code does *NOT* do that. I leave that as an exercise for the reader.

    But it does encode one character at a time, converting letters (only) to the next letter in sequence (and Z to A). It also doesn't handle the shift or CTRL keys, so you can get some strange results.

    But at least it shows you the basics of doing character by character in an <input> field.

    Tested in FF and MSIE 7 and Chrome.
    Code:
    <html>
    <head>
    <script type="text/javascript">
    var keyCodeA = "A".charCodeAt(0);
    var keyCodeZ = "Z".charCodeAt(0);
    
    function doKey( fld, evt )
    {
        var key;
        if ( evt == null ) /* msie only */
        {
            key = window.event.keyCode;
            window.event.cancelBubble = true;
        } else { /* other browsers [and sometimes MSIE 9...depends] */
            key = evt.keyCode;
            evt.stopPropagation();
            evt.preventDefault()
        }
        if ( key >= keyCodeA && key < keyCodeZ ) ++key
        else if ( key == keyCodeZ ) key = keyCodeA;
        fld.value += String.fromCharCode(key);
        return false;
    }
    
    
    function setUp()
    {
        document.forms[0].joy.onkeydown = 
            function(evt) { return doKey( this, evt ); }
    }
    window.onload = setUp;
    
    </script>
    <body>
    <form>
    Type something here: <input name="joy" size="50"/>
    </form>
    </body>
    </html>
    Last edited by Old Pedant; 10-16-2011 at 05:29 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •