Do-While loop issue

trouble with running a do-while loop

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 3849 Views - Last Post: 09 July 2010 - 09:22 AM

#1 monkadelicd  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Do-While loop issue

Posted 08 July 2010 - 02:15 PM

I am working through a Javascript tutorial site that has exercises accompanied with each lesson. Until yesterday the exercises didn't include using anything that wasn't covered in the lessons.

I just got through with the lesson on loops. One of the exercises wants me to write a script that will accept a string of numbers, one at a time, from the user then calculate the sum, average, smallest number, and largest number.

So I'm not sure how to go about this but I'm just going to try and work through it. Any clues here would be great.

The trouble I'm having is with my first attempt. I'm going to try a do-while loop, with nested loops probably. so far I've got this

do
	{
		number = parseFloat(prompt("Enter a number from the series to be averaged.", ""));
		s = prompt("Are there more numbers to enter? Enter Y or N.", "");
		seq_length++;
	}
while (s = "Y" || s = "y");



I've declared all the variables above this. Itwouldn't run initially so I started commenting out lines. I found that continue isn't a valid variable. I changed that but I still can't get the do-while block to run. If I comment out all lines except 3, 4, and 5 it works fine.
When I take out the comments the bock doesn't run.

Is This A Good Question/Topic? 0
  • +

Replies To: Do-While loop issue

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,439
  • Joined: 08-June 10

Re: Do-While loop issue

Posted 08 July 2010 - 03:14 PM

continue is a keyword similar to break.

the only problem I see in your code is, that you don’t save the numbers anywhere.

I recommend an array, with some neat tricks you can calculate the average very easy.

This post has been edited by Dormilich: 08 July 2010 - 03:15 PM

Was This Post Helpful? 1
  • +
  • -

#3 husseycoding  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 52
  • View blog
  • Posts: 174
  • Joined: 23-June 10

Re: Do-While loop issue

Posted 08 July 2010 - 03:25 PM

Your starting point looks good, the next step might be to place the numbers entered into an array to carry out the calculations on.
Was This Post Helpful? 0
  • +
  • -

#4 monkadelicd  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Re: Do-While loop issue

Posted 08 July 2010 - 03:34 PM

View Posthusseycoding, on 08 July 2010 - 02:25 PM, said:

Your starting point looks good, the next step might be to place the numbers entered into an array to carry out the calculations on.


I was assuming the problem could be resolved without an array. The lessons haven't covered arrays yet so I'm assuming it can be done. The exercise is worded in a way that leads me to believe that I am not to specify a limit on numbers in the series. I assumed arrays could do this but is there another way.

I'm stuck on figuring out how to save the numbers without knowing how many numbers there will be. Variables can't be created dynamically can they?

Oh yeah! The real problem is that code doesn't run. Here is the whole HTML document:
<html>
<body>
<script language="Javascript" type="text/javascript">
<!--
var number;
var average;
var sum;
var smallest;
var largest;
var s;
var seq_length = 0;


do
	{
		number = parseFloat(prompt("Enter a number from the series to be averaged.", ""));
		s = prompt("Are there more numbers to enter? Enter Y or N.", "");
		seq_length++;
	}
while (s = "Y" || s = "y");

document.write(number + "<br />" + seq_length + "<br />" + s);
//-->
</script>
</body>
</html>


This post has been edited by monkadelicd: 08 July 2010 - 03:38 PM

Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,439
  • Joined: 08-June 10

Re: Do-While loop issue

Posted 08 July 2010 - 03:39 PM

View Postmonkadelicd, on 08 July 2010 - 10:34 PM, said:

I was assuming the problem could be resolved without an array. The lessons haven't covered arrays yet so I'm assuming it can be done. The exercise is worded in a way that leads me to believe that I am not to specify a limit on numbers in the series. I assumed arrays could do this but is there another way.

you might solve it without arrays, but arrays are best suited for that task.

View Postmonkadelicd, on 08 July 2010 - 10:34 PM, said:

I'm stuck on figuring out how to save the numbers without knowing how many numbers there will be. Variables can't be created dynamically can they?

nearly everything in Javascript can be created dynamically. for instance, arrays don’t have a fixed length.

you should read about arrays, they are extremely useful.

by using arrays you don’t even need seq_length (because you can use the array’s length)

This post has been edited by Dormilich: 08 July 2010 - 03:44 PM

Was This Post Helpful? 0
  • +
  • -

#6 monkadelicd  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Re: Do-While loop issue

Posted 08 July 2010 - 03:47 PM

I understand that arrays are probably the best way to get this accomplished. What I want to know is if there is another way; whether it involves more code or not.

The lessons haven't covered arrays yet so I am assuming the author expects me to get it done without arrays. I will try and contact the author to see if I can get a hint.

I wish the answers were available on the site but I don't think the author posted them anywhere.

Thanks for the recommendations.

Is there an issue with sum being a variable? I noticed it is colored when I posted the code here.
Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,439
  • Joined: 08-June 10

Re: Do-While loop issue

Posted 08 July 2010 - 03:49 PM

ha!

hint: try the += operator.

simple, but not very versatile solution.

This post has been edited by Dormilich: 08 July 2010 - 03:51 PM

Was This Post Helpful? 1
  • +
  • -

#8 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 446
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Do-While loop issue

Posted 08 July 2010 - 04:08 PM

Here is a working copy of what I think you were basically trying to do. Some of the changes I made included setting a default value to each of the variables, this way when getting the value from the prompt we know we're working with two number values (the default value and the prompts return value). Otherwise it's possible that the number from the prompt may be getting converted to a string value. Then we use the value from number and add it to the value in sum. This way number keeps the value throughout each iteration which should allow you to then find the smallest and largest numbers from it. The other change I made was to move the script into a function in the head section of the page and call it when the body loads. The value is then written to a div within the body. You can change any of that as needed, but hopefully this helps some. Also, I changed your
while (s = "Y" || s = "y");
to
while (s == "Y" || s == "y");
. The first set's the value of s to "Y" or "y", the second one checks to see if it's equal to "Y" or "y".

<html>
<head>
  <script type="text/javascript">
  var number = 0;
  var average = 0;
  var sum = 0;
  var smallest = 0;
  var largest = 0;
  var s = "";
  var seq_length = 0;

function get_average(){
  do{
		number = parseFloat(prompt("Enter a number from the series to be averaged.", ""));
                sum += number;
		s = prompt("Are there more numbers to enter? Enter Y or N.", "");
		seq_length++; 
  } while (s == "Y" || s == "y");

  document.getElementById('display_area').innerHTML = (sum + "<br />" + seq_length + "<br />" + (sum/seq_length));
}
</script>
</head>
<body onload="get_average();">
  <div id="display_area"></div>
</body>
</html>



edit: tested this in firefox, can't remember if innerHTML works in IE or not

This post has been edited by Nakor: 08 July 2010 - 04:11 PM

Was This Post Helpful? 1
  • +
  • -

#9 monkadelicd  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Re: Do-While loop issue

Posted 09 July 2010 - 04:02 AM

Thanks to all for the help and recommendations. Using sum += number was a big step in the right direction. Now I just have to figure out how to get the largest and smallest number entered. Any hints or tips without using an array?
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,439
  • Joined: 08-June 10

Re: Do-While loop issue

Posted 09 July 2010 - 04:22 AM

this would require an extra function where you compare the current value with the previously smallest/biggest number. you can code that into an array, which reduces the usage to something like
var min_value = arr.min();

advantage: you do not need to do that in the while loop.

mental note for myself: Math.min()/Math.max()

This post has been edited by Dormilich: 09 July 2010 - 04:26 AM

Was This Post Helpful? 0
  • +
  • -

#11 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 446
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Do-While loop issue

Posted 09 July 2010 - 04:46 AM

not too hard to check for greatest or smallest without using an array. Just check if the value of number is less than the value of smallest, if it is then set smallest to the value of number. Do the opposite for largest.
Was This Post Helpful? 1
  • +
  • -

#12 monkadelicd  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Re: Do-While loop issue

Posted 09 July 2010 - 05:02 AM

This is what I came up with. This uses only code I am supposed to know from the lessons in the tutorial, hence the lack of arrays. What do you think? I managed to come up with this all on my own besides the sum += number part. Thanks Nakor and Dormilich

I added the do-while loop for the second prompt just to make sure the right character is entered. I didn't have line 25 and 26 in there at first and had to kill Firefox with Task Manager because it was an endless loop. But it functions as the homework question asks now.

I used the "NULL" value for largest and smallest for lack of another idea. I couldn't initialize those two variables as 0 values because I wouldn't have been able to properly evaluate whether number was larger or smaller than any other number.

Any feedback is appreciated. I'm taking programming more serious than ever before finally. It's something I've always wanted to learn but just couldn't focus on.

<html>
<body>
<script language="Javascript" type="text/javascript">
<!--
var number = 0;
var average = 0;
var sum = 0;
var smallest = "NULL";
var largest = "NULL";
var s;
var seq_length = 0;


do {
		number = parseFloat(prompt("Enter a number from the series to be averaged.", ""));
			sum += number;
			if (smallest == "NULL" || smallest > number)
				smallest = number;

			if (largest == "NULL" || largest < number)
				largest = number;				

			do {
				s = prompt("Are there more numbers to enter? Enter Y or N.", "");
				if (s == "Y" || s == "y" || s == "N" || s == "n")
					break;
			}
			while (s != "Y" || s != "y" || s != "N" || s != "n");
		seq_length++;
}
while (s == "Y" || s == "y");

document.write("sum = " + sum + "<br />largest = " + largest + "<br />smallest = " + smallest + "<br />average = 

" + (sum / seq_length) + "<br />s = " + s);
//-->
</script>
</body>
</html>



For the record I had the code done before your last post Nakor! ;) Same solution. Thanks, though.
Was This Post Helpful? 0
  • +
  • -

#13 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,439
  • Joined: 08-June 10

Re: Do-While loop issue

Posted 09 July 2010 - 05:04 AM

you can use Number.MAX_NUMBER or Number.POSITIVE_INFINITY as starting value for the smallest number. (and the appropriate versions for the largest number). then you only need if (smallest > number) (any valid number is smaller than positive infinity or the maximal valid value).
Was This Post Helpful? 0
  • +
  • -

#14 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 446
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Do-While loop issue

Posted 09 July 2010 - 05:48 AM

View Postmonkadelicd, on 09 July 2010 - 05:02 AM, said:

do {  
    s = prompt("Are there more numbers to enter? Enter Y or N.", "");  
    if (s == "Y" || s == "y" || s == "N" || s == "n")  
        break;  
   } while (s != "Y" || s != "y" || s != "N" || s != "n");  





You should be able to get rid of the if statement, the while statement is checking the same thing.
Was This Post Helpful? 0
  • +
  • -

#15 monkadelicd  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Re: Do-While loop issue

Posted 09 July 2010 - 06:17 AM

View PostNakor, on 09 July 2010 - 04:48 AM, said:

View Postmonkadelicd, on 09 July 2010 - 05:02 AM, said:

do {  
    s = prompt("Are there more numbers to enter? Enter Y or N.", "");  
    if (s == "Y" || s == "y" || s == "N" || s == "n")  
        break;  
   } while (s != "Y" || s != "y" || s != "N" || s != "n");  





You should be able to get rid of the if statement, the while statement is checking the same thing.


Before I put in the if statement the do-while loop would just repeat if any of the proper characters were entered.

So basically the do-while condition says keep running if y or n is not entered and the if statement says stop if y or n is entered.

Am I looking at this wrong?

This post has been edited by monkadelicd: 09 July 2010 - 06:18 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2