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 6 of 6
  1. #1
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    882
    Thanks
    53
    Thanked 6 Times in 5 Posts

    if else error I cannot find

    When I post this code into The Online Lint it shows an error (in red below).

    But I have counted and recounted those curly braces a hundred times.

    Can anyone see what might be causing the error?

    Code:
    function loseMypreset(value, box) {
    var v = value;
    var store = readCookie('mycookie');
    var mysession = store.substr((v*35), 35); // the chosen session
    var x = store.search(mysession);
    var state = box.checked;  // true/false
    	if (state) { // box is checked
    	ans = confirm('Are you sure you want to restore the checkbox?');
    	}
    		if (ans) { // if OK
    		// RESTORE SESSION
    		var bu = readCookie('mybackup'); // restore a deleted session
    		for(i=0; i < bu.length; i += 36) { // 36 because backup stores (v + mysession)
    		var z = bu.substr(i, 1);
    			if (z == v){
    			alert(z); break;
    			}  // break out of loop
    		}
    		var myrestore = bu.substr(i, 36);
    		var y = bu.search(myrestore);
        	        var store0 = bu.substr(0, y);
    		var store2 = bu.substr(y+36, bu.length);
    		var store1 = store0 + store2;
    		createCookie('mybackup',store1,9999); // delete myrestore from mybackup and save mybackup
    		myrestore = myrestore.substr(1,35); // remove the first char which saves position of session in mybackup
    		alert(myrestore);
    		alert("x = " + x);
    		var store = readCookie('mycookie');
        	        var store0 = store.substr(0, x);
    		var store2 = store.substr(x+35, store.length);
    		var store1 = store0 + myrestore + store2;
    		alert(store1);
    		createCookie('mycookie',store1,9999); // save myrestore into mycookie and save mycookie
    		}
    		return;			
    	}
    	else { // cancel
    	box.checked = false;
    	return;
    	}
    	
    	if (!state) { // box is unchecked
    	ans = confirm('Are you sure you want to delete the session?');
    	}
    	if (ans) { // if OK
    	// WIPE SESSION
    	box.checked = false;
    	var store0 = store.substr(0, x);
    	var store2 = store.substr(x+35, store.length);
    	var wipe = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"; // 35 chars
    	var store1 = store0 + wipe + store2;
    	createCookie('mycookie',store1,9999); // wipes the chosen session with z
    	var bu = readCookie('mybackup');
    	// alert(v);
    		if(! bu) { // check if backup is null
    		alert("v + mysession = " + v + mysession);
    		createCookie('mybackup',v + mysession,9999); // save the position and data of the deleted session
    		}
    		else {
    		bu += (v + mysession);
    		alert("bu = " + bu);
    		createCookie('mybackup',bu,9999); // append backup session
    		}
    		return;			
    	}
    	else { // cancel
    	box.checked = true;
    	return;
    	}
    }
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,984
    Thanks
    56
    Thanked 557 Times in 554 Posts
    Code:
        var store2 = store.substr(x+35, store.length);
            var store1 = store0 + myrestore + store2;
            alert(store1);
            createCookie('mycookie',store1,9999); // save myrestore into mycookie and save mycookie
            }
            return;            
        }
        else { // cancel
        box.checked = false;
        return;
        }
    just taking a flying guess - it seems you need to delete the one in red

  • Users who have thanked xelawho for this post:

    tpeck (08-06-2014)

  • #3
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    882
    Thanks
    53
    Thanked 6 Times in 5 Posts
    You are right. I don't know how I counted wrongly. I spent a couple of hours deleting and reinstating the if/else blocks!

    Thanks for taking a look!
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,984
    Thanks
    56
    Thanked 557 Times in 554 Posts
    No worries. You can save yourself some grief in the future using a dedicated code editor (I use notepad++ , which is free) - when you click on an opening brace, it highlights its corresponding closing brace, making problems like the above (relatively) easy to solve.

  • #5
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    882
    Thanks
    53
    Thanked 6 Times in 5 Posts
    Thanks, I didn't know that. Very useful indeed!
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,173
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    Like xelawho I use and recommend Notepad++. But you could have found this error quickly by using your error console (F12 key).

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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