having issues with while loop

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1319 Views - Last Post: 01 November 2012 - 01:04 PM

#1 mklein52   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-October 12

having issues with while loop

Posted 31 October 2012 - 06:34 PM

Create a program that takes two integers, and output all the odd numbers between the two integers on the web page. E.g. if the user enters 10 and 23, the output should be

The odd numbers between 10 and 23 are:
11, 13, 15, 17, 19, 21, 23

Your program needs to determine which number is smaller so as to set that number as the starting point. e.g. If the user enters 23 as the first number and 10 as the second number, the output will still be:
11, 13, 15, 17, 19, 21, 23

You program MUST use the while loop strucutre.

this is what i have and its not working please help me fix it


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

<html>
	<head>
		<title> While Loop </title>
	</head>
	
	<body>
	<script type="text/javascript">
	
	i=1;
				while(i >= 23){
				alert('i');
				i+=2;
				}
	
	</script>
		<p>
		
	
				</p>
		<div id="outputDiv">
		
		
		<h1 style="text-align:center"> While Loop </h1>
			<br>
		 <input type="button" id="names" size=14 value="click here">
				
		</div>
		</body>
</html>

This post has been edited by Dormilich: 01 November 2012 - 12:13 AM
Reason for edit:: please use [code] [/code] tags when posting code


Is This A Good Question/Topic? 0
  • +

Replies To: having issues with while loop

#2 JMRKER   User is offline

  • D.I.C Addict
  • member icon

Reputation: 134
  • View blog
  • Posts: 868
  • Joined: 25-October 08

Re: having issues with while loop

Posted 31 October 2012 - 06:57 PM

According to your instructions...

Quote

If the user enters 23 as the first number and 10 as the second number, the output will still be:

... where are you accepting the user's input?

What you have 'hard'-coded starts at 1 and only goes to 23.
I'm assuming the problem allows for any start and stop entries.
Was This Post Helpful? 0
  • +
  • -

#3 mklein52   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-October 12

Re: having issues with while loop

Posted 31 October 2012 - 07:02 PM

can you show me how to fix it?
Was This Post Helpful? 0
  • +
  • -

#4 JMRKER   User is offline

  • D.I.C Addict
  • member icon

Reputation: 134
  • View blog
  • Posts: 868
  • Joined: 25-October 08

Re: having issues with while loop

Posted 31 October 2012 - 07:20 PM

View Postmklein52, on 31 October 2012 - 08:02 PM, said:

can you show me how to fix it?

Yes, but as a homework assignment you need to at least make an attempt.
Was This Post Helpful? 1
  • +
  • -

#5 Nakor   User is offline

  • Professional Lurker
  • member icon

Reputation: 448
  • View blog
  • Posts: 1,504
  • Joined: 28-April 09

Re: having issues with while loop

Posted 31 October 2012 - 07:25 PM

another problem I see

while(i >= 23){


You're starting i at 1 but your condition is saying while i is greater than or equal to 23, which it won't be since i is equal to 1.

To take user input you'll need to have two text boxes on your page, one for the starting value and one for the end value. You should probably also have a button that the user can click to execute the function. You'll need to get the values from the text boxes and store the values in some variables. Then use the variables in the where condition.
Was This Post Helpful? 0
  • +
  • -

#6 mklein52   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-October 12

Re: having issues with while loop

Posted 31 October 2012 - 07:50 PM

this is what i have i cant get the alert to pop up to show the loop

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

<html>
	<head>
		<title> While Loop </title>
	</head>
	
	<body>
	<script type="text/javascript">
	
	i=1;
				while(i >= 23){
				alert('i');
				i+=2;
				}
	
	</script>
		<p>
		
	
				</p>
		<div id="outputDiv">
		
		
		<h1 style="text-align:center"> While Loop </h1>
			<br>
		  enter here: <input type="text" id="naming" size=14 value="">
		  <input type="button" value="submit" onclick=""
		  i=1>
		</div>
		</body>
</html>


View PostNakor, on 31 October 2012 - 07:25 PM, said:

another problem I see

while(i >= 23){


You're starting i at 1 but your condition is saying while i is greater than or equal to 23, which it won't be since i is equal to 1.

To take user input you'll need to have two text boxes on your page, one for the starting value and one for the end value. You should probably also have a button that the user can click to execute the function. You'll need to get the values from the text boxes and store the values in some variables. Then use the variables in the where condition.


so should it be a less than symbol or just do == sign?

This post has been edited by Dormilich: 01 November 2012 - 12:15 AM
Reason for edit:: please use [code] [/code] tags when posting code

Was This Post Helpful? 0
  • +
  • -

#7 JMRKER   User is offline

  • D.I.C Addict
  • member icon

Reputation: 134
  • View blog
  • Posts: 868
  • Joined: 25-October 08

Re: having issues with while loop

Posted 31 October 2012 - 08:00 PM

The only thing the alert will display will be 'i'.
But it does not get there due to other errors.
If you are using FF or Chrome browsers, look at the Error Console for some help.
Your submit will not work, because the onclick function is not defined.

BTW, you should use [code] and [/code] tags
around your script to allow others to more easily read, copy, test and debug
your program.

This post has been edited by Dormilich: 01 November 2012 - 12:16 AM
Reason for edit:: added link for error console

Was This Post Helpful? 1
  • +
  • -

#8 JMRKER   User is offline

  • D.I.C Addict
  • member icon

Reputation: 134
  • View blog
  • Posts: 868
  • Joined: 25-October 08

Re: having issues with while loop

Posted 31 October 2012 - 08:17 PM

Getting close to my bedtime.
I'll check tomorrow is see if you have solved your problem.

Some other considerations:
1. If you allow user to enter the numbers, you will need to check for number entries only.
2. If you allow user to enter the numbers, you will need to check that one number is smaller than the other. If not, you may get no display.
3. If you increment you while loop by i+=2, you will never get a display if the user enters an even number to begin
4. I don't believe the submit will work without a <form> tag assignment.
You can make the program work without the <form> or type="submit" at all.
5. If you start with i=1, then the alert will never execute because it is not called until i is >= 23. But the loop only executes while i is < 23 due to your logic.

This post has been edited by JMRKER: 31 October 2012 - 08:18 PM

Was This Post Helpful? 0
  • +
  • -

#9 mklein52   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-October 12

Re: having issues with while loop

Posted 31 October 2012 - 08:30 PM

is there a way you could show me an example of one of these?
Was This Post Helpful? 0
  • +
  • -

#10 Nakor   User is offline

  • Professional Lurker
  • member icon

Reputation: 448
  • View blog
  • Posts: 1,504
  • Joined: 28-April 09

Re: having issues with while loop

Posted 31 October 2012 - 09:10 PM

You need to have you javascript code in a function

<script>
function myFunction(){
// code goes here
}
</script>



Then your button would look like

<input type='button' value='submit' onclick='myFunction()' />



You still need to make the suggested changes to the condition in your while loop as well

This post has been edited by Nakor: 31 October 2012 - 09:12 PM

Was This Post Helpful? 1
  • +
  • -

#11 mklein52   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-October 12

Re: having issues with while loop

Posted 01 November 2012 - 06:29 AM

this is what i have got now

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

<html>
	<head>
		<title> While Loop </title>
	</head>
	
	<body>
	<script type="text/javascript">
	function mine(){
	//i=13;
				while(i <= 23)
				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>



Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: having issues with while loop

Posted 01 November 2012 - 06:42 AM

Look closely at the onclick example given to you by Nakor and compare it to what you have. Do they look the same (except for the function name)?
Was This Post Helpful? 0
  • +
  • -

#13 mklein52   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-October 12

Re: having issues with while loop

Posted 01 November 2012 - 06:44 AM

onclick='mine()' is that how it is supposed to be
Was This Post Helpful? 0
  • +
  • -

#14 Nakor   User is offline

  • Professional Lurker
  • member icon

Reputation: 448
  • View blog
  • Posts: 1,504
  • Joined: 28-April 09

Re: having issues with while loop

Posted 01 November 2012 - 06:44 AM

also remove the single quotes from around the i in your alert. Right now you would just alert the letter i instead of the value in the i variable
Was This Post Helpful? 0
  • +
  • -

#15 mklein52   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-October 12

Re: having issues with while loop

Posted 01 November 2012 - 06:50 AM

ok now it is just showing 13 in the dialog box
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2