www.webdeveloper.com
Results 1 to 3 of 3

Thread: proper line termination syntax

  1. #1
    Join Date
    Apr 2012
    Location
    Porstmouth, NH
    Posts
    20

    proper line termination syntax

    I am new to JavaScript and am copying and pasting scripts and have one that works in IE9, FF, Chrome, but not Safari. The code initially wasn't working in Chrome but cleaning up this and that (like closing tags properly such as ">" to " />" and adding a semi-colon after things like document.cookie="ready=yes";) has it working now in Chrome.

    In looking at this quiz's main JS as well as a page level JS, it seems like the lines of code are not properly closed with things like semi-colons. I don't know if that has anything to do with the Safari issue but I want to learn to code neatly.

    If you could show me what I need to add, I'd appreciate it and if you happen to see something that screams out a Safari issue, let me know! Thanks! =)

    Here are two snippets that I think should have some end of line syntax additions:

    Code:
    <script type="text/javascript">
    function gradeit(){
    var incorrect=null
    for (q=1;q<=totalquestions;q++){
    	var thequestion=eval("document.myquiz.question"+q)
    	for (c=0;c<thequestion.length;c++){
    		if (thequestion[c].checked==true)
    		actualchoices[q]=thequestion[c].value
    		}
    	if (actualchoices[q]!=correctchoices[q]){
    		if (incorrect==null)
    		incorrect=q
    		else
    		incorrect+="/"+q
    		}
    	}
    if (incorrect==null)
    	incorrect="a/b"
    	document.cookie='q='+incorrect
    if (document.cookie=='')
    	alert("Your browser does not accept cookies. Please adjust your browser settings.")
    	else
    window.location="results.htm"
    }
    </script>
    and

    Code:
    <script type="text/javascript">
    var wrong=0
    	for (e=0;e<=2;e++)
    		document.result[e].value=""
    var results=document.cookie.split(";")
    	for (n=0;n<=results.length-1;n++){
    		if (results[n].charAt(1)=='q')
    			parse=n
    }
    var incorrect=results[parse].split("=")
    	incorrect=incorrect[1].split("/")
    	if (incorrect[incorrect.length-1]=='b')
    		incorrect=""
    		document.result[0].value=totalquestions-incorrect.length+" out of "+totalquestions
    		document.result[2].value=(totalquestions-incorrect.length)/totalquestions*100+"%"
    	for (temp=0;temp<incorrect.length;temp++)
    		document.result[1].value+=incorrect[temp]+", "
    </script>

  2. #2
    Join Date
    Nov 2010
    Posts
    1,083
    some things are mandatory, some are helpful (because they let you see more clearly what the code is doing) and others are just preference. Here's how I would mark up your second piece of code (if I understand it correctly) which I suspect I don't as I don't really know what your loops are supposed to be doing (see point 2, above)...

    Code:
    <script type="text/javascript">
    var wrong=0;
    	for (e=0;e<=2;e++) {
    		document.result[e].value="";
    var results=document.cookie.split(";");
    	for (n=0;n<=results.length-1;n++){
    		if (results[n].charAt(1)=='q') {
    			parse=n;
    			}
    	}
    }
    var incorrect=results[parse].split("=");
    	incorrect=incorrect[1].split("/");
    	if (incorrect[incorrect.length-1]=='b'){
    		incorrect="";
    		document.result[0].value=totalquestions-incorrect.length+" out of "+totalquestions;
    		document.result[2].value=(totalquestions-incorrect.length)/totalquestions*100+"%";
    		}
    	for (temp=0;temp<incorrect.length;temp++){
    		document.result[1].value+=incorrect[temp]+", ";
    		}
    		</script>

  3. #3
    Join Date
    Apr 2012
    Location
    Porstmouth, NH
    Posts
    20
    thanks xelawho! i figured it "should" have closing brackets but since it works as it is, it was confusing.

    This code is for a multiple choice quiz where the answers are in an array in the JavaScript.

    So it looks like semi-colons should be placed at the end of most lines - thanks xelawho, this helps me quite a bit and I'll see what happens whn I mess with the first function!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles