Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 16th, 2011, 02:00 AM
Authorized User
 
Join Date: Sep 2008
Posts: 54
Thanks: 8
Thanked 1 Time in 1 Post
Default What r the values of for loop and if statement?

Hi!


I have written the following program from a book, this program is basically for an exception, first I don’t understand the follow of this program, specially the values in for loop and in if statement, secondly the only output is this script and nothing is happening, in Red I am writing what I understand and after the program I have few questions:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>Exception test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script language="Javascript">
function primeTest(n)
{
document.write ("Testing"+n+":")
try{
if (n<1 || n>20) throw "It's out of range" //n is less than 1 or greater than 20
for(var i =2; i <n; ++i) //I is equal to 2 increment in I till I is less than n
if (n% i ==0) throw "It's divisible by"+ //n divide by 0 reminder 0, then "It's divisible by"
document.writeln("It's prime. <br>") //if 0 then its prime
}
catch(exception){ //otherwise catch
document.writeln(exception+'<br>') //Write exception
}
}
</script>
<meta content="MSHTML 6.00.2900.6082" name="GENERATOR"></head>
<body>
<p>this script </p>
<script language="javascript">
<!-
for(i=0;i<=21;++i) //this is comment
{
primeTest(i)
}
-></script>
</body></html>


Questions:
1-At 1st what is the value of n? Since we have not declared it?
2-The 1st value of I is 2?


Thanks in advance

__________________
How to do programming?
 
Old June 16th, 2011, 04:32 AM
Authorized User
 
Join Date: Jan 2011
Posts: 86
Thanks: 1
Thanked 12 Times in 12 Posts
Default

Hi arbab,

1> the value of n is what you pass to it in the for loop: primeTest(i) and the for loop goes from zero to twenty-one
2> the value of i in the for loop goes from zero to twenty-one, so the frist value is zero (the increment only happens at the end of the for loop)
2> but wait... they have declared a new i within the primeTest function... this (function-scope) variable i starts indeed at two
 
Old June 27th, 2011, 05:11 PM
Authorized User
 
Join Date: Sep 2008
Posts: 54
Thanks: 8
Thanked 1 Time in 1 Post
Default when the exception will be thrown and catch?

Primetest is the function which takes n as argument, but n is used within the function of primetest, so is it n 1st calculated from primetest function then gives value to primetest function.

Please explain one flow of the program, I still don’t get when the exception will be thrown and catch?
__________________
How to do programming?
 
Old June 29th, 2011, 06:44 AM
Authorized User
 
Join Date: Jan 2011
Posts: 86
Thanks: 1
Thanked 12 Times in 12 Posts
Default

Hi,

- the primeTest is called 22 times (from i=0 up to i = 21)
- in its first and last call (so when i = 0 and when i = 21) it will throw the exception "It's out of range"
- in the other calls it will throw the exception "It's divisible by" for non-prime numbers only

Hope this helps.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to concatenate values in a loop LeoMathew XSLT 6 February 14th, 2008 09:03 AM
How to loop for Select Values rohit_ghosh Excel VBA 3 May 8th, 2007 03:29 PM
Regarding loop statement kalpesh.mahajan ASP.NET 1.0 and 1.1 Professional 2 August 30th, 2006 04:55 AM
Need help in retrieving values from a loop ! back2grave XSLT 0 June 27th, 2006 05:29 PM
loop values and text box values move mateenmohd Classic ASP Basics 2 April 5th, 2005 11:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.