11 Replies - 3450 Views - Last Post: 09 November 2012 - 12:35 AM

#1 ghukill88   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-November 12

Using the for loop

Posted 08 November 2012 - 07:26 AM

For loop to write a program that takes two integers, and output a summation of all the integers between (including) the two integers.
if the user enteres 1 and 100, the output should be

The summation of all the integers between 1 and 100 is 5050
Is This A Good Question/Topic? 0
  • +

Replies To: Using the for loop

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4217
  • View blog
  • Posts: 13,328
  • Joined: 08-June 10

Re: Using the for loop

Posted 08 November 2012 - 07:31 AM

you are aware that there is no need to use a loop for that?

otherwise, what have you tried?
Was This Post Helpful? 0
  • +
  • -

#3 ghukill88   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-November 12

Re: Using the for loop

Posted 08 November 2012 - 07:34 AM

well i have tried this

<!doctype.html>
<!-- Lab #9 while loop -->

<html>
	<head>
		<title> While Loop </title>
	</head>
	
	<body>
	<script type="text/javascript">
	function mine(){
	
				for(i=1;i<=100;i++)
				alert('i');
				i+=2;
	}
	
				
	
	</script>
		
		<div id="outputDiv">
		
		
		<h1 style="text-align:center"> While Loop </h1>
			<br>
		  first imput: <input type="text" id="naming" size=14 value="">
		  <br>
		  second imput:<input type="text" id="naming2" size=14 value="">
		  <input type="button" value="submit" onclick='mine'
		  />
		</div>
		</body>
</html>


MOD EDIT: Added code tags. When posting code...USE CODE TAGS!!!

:code:
Was This Post Helpful? 0
  • +
  • -

#4 Kruithne   User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: Using the for loop

Posted 08 November 2012 - 07:34 AM

Hello,

I've prepared a small snippet below to show you how a function like this would work and poked in some comments so you can see each step of the process to achieve this.
function calculateALLTheNumbers(lower, upper)
{
	var total = 0; // Start with 0, the very best place to start.
	
	for (var i = lower-1; i < upper+1; i++) // Loop through all the things. We expand the bounds a little so the output matches.
		total += i; // Add the currnet number to the total.
		
	return total; // Return what we have left over.
}

I don't like giving away code in such a manner but for such a thing it seemed pointless to explain (make a loop and add some numbers together) rather than just give you a working snippet. Hopefully you will take the above example as a chance to learn how it works rather than copy and paste.

Check out this page for future reading on loops.

EDIT: I do agree with Dormilich on this, you do not need to use a loop for this. But you didn't ask for a better method so, huzzah!

This post has been edited by Kruithne: 08 November 2012 - 07:35 AM

Was This Post Helpful? 0
  • +
  • -

#5 ghukill88   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-November 12

Re: Using the for loop

Posted 08 November 2012 - 07:37 AM

just kidding this is right one clicked wrong program
<!doctype.html>
<!-- Lab #9 for loop -->

<html>
	<head>
		<title> For Loop </title>
	</head>
	
	<body>
	<script type="text/javascript">
	function mine(){
	
				for(i=1;i<=100;i++)
				
				
	}
	
				
	
	</script>
		
		<div id="outputDiv">
		
		
		<h1 style="text-align:center"> For Loop </h1>
			<br>
		  first imput: <input type="text" id="naming" size=14 value="">
		  <br>
		  second imput:<input type="text" id="naming2" size=14 value="">
		  <input type="button" value="submit" onclick='mine'
		  />
		</div>
		</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#6 Kruithne   User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: Using the for loop

Posted 08 November 2012 - 07:39 AM

Quote

just kidding this is right one clicked wrong program


I don't think you know what 'just kidding' means. Check my above response for a solution.
Was This Post Helpful? 0
  • +
  • -

#7 ghukill88   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-November 12

Re: Using the for loop

Posted 08 November 2012 - 07:46 AM

The thing I was taught was you needed to have a start, end, and how it changes in the same parentheses, but I don't get how to execute the addition of all numbers in between
Was This Post Helpful? 0
  • +
  • -

#8 Kruithne   User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: Using the for loop

Posted 08 November 2012 - 07:51 AM

View Postghukill88, on 08 November 2012 - 02:46 PM, said:

The thing I was taught was you needed to have a start, end, and how it changes in the same parentheses, but I don't get how to execute the addition of all numbers in between


Look at the snippet of code I posted, it's all there. It explains everything.
Was This Post Helpful? 0
  • +
  • -

#9 ghukill88   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-November 12

Re: Using the for loop

Posted 08 November 2012 - 11:39 AM

okay got all that figured out but now i have to do like a max and min
Was This Post Helpful? 0
  • +
  • -

#10 ghukill88   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-November 12

Re: Using the for loop

Posted 08 November 2012 - 12:26 PM

I've tried using var min and max in the function. Then I tried putting the 0 and 100 in the bottom portion of the function
Was This Post Helpful? 0
  • +
  • -

#11 ivaylo9603   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 05-November 12

Re: Using the for loop

Posted 08 November 2012 - 12:59 PM

This function should be used:
function mine( ){

  var min=document.naming.value;
  var max=document.naming2.value;
  var res=0;

  for( int i=min;i<max;i++ ){
    res += i;
  } 
  
  alert(res); 
}



Along with this button:
<input type="button" value="submit" onclick="mine( )"/>


Explaination in the previous topic.

This post has been edited by ivaylo9603: 08 November 2012 - 01:01 PM

Was This Post Helpful? 0
  • +
  • -

#12 laytonsdad   User is offline

  • Let it rip!
  • member icon

Reputation: 466
  • View blog
  • Posts: 1,992
  • Joined: 30-April 10

Re: Using the for loop

Posted 09 November 2012 - 12:35 AM

http://www.tizag.com...vascriptfor.php
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1