Jump to content

Need help: 'manually tracing' js with alert etc.?


Recommended Posts

Hello & Thanks , I am trying to manually trace (alert !) when things get executed .The first thing i notice is that

Hello World !

Then (Enter a number)

	number = prompt("Enter a number:", "5") 	document.write(Factorial(number));

Gets executed next .?? Does that means that 'everything js' outside of the Function gets executed first ???? sort of an initialization of the js code 1st?? Then:

var r=confirm("CONFIRM: aNumber = "+aNumber+"   " +"OK, keep going , or CANCEL ");

Gets executed TWICE , no matter what I click on OK or CANCEL . Also , here:

		alert("aNumber is less than Zero !") 		return "undefined";

return "undefined";

gets executedButalert("aNumber is less than Zero !") does not .Pls , can someone show me what the proper code should be ?Also , how can I get each .innerHTML and document.write on a separate line , rather than write on a new page ? I would like to see all Trace stuff on the same page . Thanks...vm

<!DOCTYPE html><html>  <head>	<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">	<title></title>	<meta content="vmars316" name="author">  </head>  <body onload="Factorial(4)">  <h3>Hello World !</h3>  <p id="demo"> </p>  <script>  var TimesIn =0;  function Factorial(aNumber){	{	var x;	var r=confirm("CONFIRM: aNumber = "+aNumber+"   " +"OK, keep going , or CANCEL ");	if (r==true)	  {	  x=(".innerHTML: x =  "+"aNumber = "  +aNumber+"   " +"OK , keep on going!") ;	  document.getElementById("demo").innerHTML=x;	  }	else	  {	  x="x = "+aNumber+"   Cancel , it is !";	  document.getElementById("demo").innerHTML=x;	  return;  // Exit Function	  }	  document.getElementById("demo").innerHTML=x;	}// If the number is not an integer, round it down.	aNumber = Math.floor(aNumber);	TimesIn = TimesIn + TimesIn + 1;	document.write("TimesIn = " + TimesIn) ;	 alert()// The number must be equal to or bigger than zero	if (aNumber < 0)	{		alert("aNumber is less than Zero !")		return "undefined";	}	if ((aNumber == 0) || (aNumber == 1))	{ // If the number is 0 or 1, its Factorial is 1.		alert("aNumber is 0 or 1, its Factorial is 1 !")		return 1;	}	else	{ // Make a recursive call		alert("aNumber * Factorial(aNumber - 1")		return (aNumber * Factorial(aNumber - 1));	}} // End of Factorial	number = prompt("Enter a number:", "5")	document.write(Factorial(number));  </script>	<p><br>	</p>  </body></html>

Edited by vmars316
Link to post
Share on other sites

1. Everything in your script that is not in a function is executed as soon as your browser reads it. So those last two statements are executed immediately. 2. The first call to document.write happens while the page is still loading, so it writes text into the document. When it is called in the function, AFTER the page loads, document.write KILLS the current document and begins writing a new one. There is no way to change that. This is why very few programs use document.write. Use innerHTML exclusively if you want a dynamic report of the function's progress. 3. The best way to start all this is to provide a text input for the user to enter a number at a time of his choosing. (I mean, get read of that confirm-type stuff.) Add a button with its click event bound to the Factorial function. Look up event handling.

Edited by Deirdre's Dad
  • Like 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...