7 Replies - 943 Views - Last Post: 01 May 2012 - 04:44 PM

#1 Jagst3r15  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 21-December 10

Write the average and all numbers less than average

Posted 30 April 2012 - 08:46 PM

Hi, so the title is pretty self explanatory. I am working on this code and would only post if I was stuck - which I am. I would appreciate some help in trying to write the second loop to display all the numbers less than the average (see the commented section I have below; I know what I need to do, I just can't wrap my head around writing that second loop! :().

<html>
 <body>
 <script>
  var nums = new Array();
var num = 0;
var k;
var counter = 0;
var sum = 0;
var avg;

while (num != 999 && counter < 10) {
    num = prompt("Enter 10 numbers", "");
    num = parseInt(num);
    if (counter != 10) {
        nums[counter] = num; // store the value in the array
        counter = counter + 1;
    }
}
// Find the sum
for (k = 0; k < counter; k = k + 1 ) {
    sum = sum + nums[k]; // keep a running total
}
if (counter != 0)
    avg = sum /counter;
else
    avg = 0;

document.write("the average is " + avg);
// Need to create another loop
// Within the loop test if nums[k] is less than the average
// If it is then add one to a counter.

if (nums[k] < avg)

document.write(nums);

</script>
 </body>
</html>




Is This A Good Question/Topic? 0
  • +

Replies To: Write the average and all numbers less than average

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5437
  • View blog
  • Posts: 11,667
  • Joined: 02-June 10

Re: Write the average and all numbers less than average

Posted 30 April 2012 - 09:02 PM

Line 15: How are you using the variable nums[] when you never created it?

Assuming nums[] contains all your numbers, and avg contains your average then wouldn't listing all the numbers less than the average just be a matter of looping through all the numbers and doing an "if x < y" type thing?

Line 20 shows you can make a for loop (or reasonably copy/paste one from someplace)

Line 33 has the if comparison..

Try putting all that together.
Was This Post Helpful? 1
  • +
  • -

#3 Jagst3r15  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 21-December 10

Re: Write the average and all numbers less than average

Posted 30 April 2012 - 09:05 PM

isn't nums the array I created? (line 4).
Was This Post Helpful? 1
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5437
  • View blog
  • Posts: 11,667
  • Joined: 02-June 10

Re: Write the average and all numbers less than average

Posted 30 April 2012 - 09:07 PM

I guess its late. Maybe I scrolled down to see the rest of the code, taking lines 1-3 out of site. My goof.
Was This Post Helpful? 0
  • +
  • -

#5 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 789
  • View blog
  • Posts: 1,676
  • Joined: 30-January 09

Re: Write the average and all numbers less than average

Posted 30 April 2012 - 09:38 PM

The second loop isn't too difficult. You need to go through each element of the array, check if it is below the average, and print it out if it is. The code for printing out the information you want is below... I've added <br> elements so that each number prints out on a new line.
document.write("the average is " + avg);
document.write("<br>");

for (k = 0; k < counter; k = k + 1)
{
	if (nums[k] < avg)
	{
		document.write(nums[k]);
		document.write("<br>");
	}
}


So basically what is happening is...:
  • You're looping through the array of entered numbers one at a time
  • If the current number is less than hte average (which you have calculated before as avg), then print out the number, and a line break

What you might want to do in your loops, though, is check whether the entered number is actually a number. You've used parseInt() earlier in your code, but if a user hits the escape key or enter during number entry, the value entered is null. If this happens, then your code won't be able to calculate the average - you'll get the average value being NaN (not a number).
Was This Post Helpful? 1
  • +
  • -

#6 Jagst3r15  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 21-December 10

Re: Write the average and all numbers less than average

Posted 30 April 2012 - 09:43 PM

Thank you both (especially eipi). That was soooo stupid on my part :dozingoff:, since I already coded much of the answer (just had to move down the counter loop I already had!). Was working on this for like 3 hours, so I appreciate the help. Frustrating when I know what I need to do, but can't figure out the coding aspects most of the time.

BTW: Your Art of War game is interesting :)

This post has been edited by Jagst3r15: 30 April 2012 - 09:54 PM

Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5437
  • View blog
  • Posts: 11,667
  • Joined: 02-June 10

Re: Write the average and all numbers less than average

Posted 01 May 2012 - 07:16 AM

View PostJagst3r15, on 30 April 2012 - 10:43 PM, said:

Thank you both (especially eipi).



View PosttlhIn`toq, on 30 April 2012 - 10:02 PM, said:

Assuming nums[] contains all your numbers, and avg contains your average then wouldn't listing all the numbers less than the average just be a matter of looping through all the numbers and doing an "if x < y" type thing?

Line 20 shows you can make a for loop (or reasonably copy/paste one from someplace)

Line 33 has the if comparison..

Try putting all that together.


View Poste_i_pi, on 30 April 2012 - 10:38 PM, said:

for (k = 0; k < counter; k = k + 1)
{
	if (nums[k] < avg)
	{
		document.write(nums[k]);
		document.write("<br>");
	}
}




Yep... Always nicer to have someone write your code for you before you've shown your own effort to follow the suggestion.

This might have helped you on this one assignments but I'm going to suggest this did you more harm than good. Being given the code doesn't help you in the long run. There is no learning taking place. No figuring it out based on a suggestion. No development of the code on your own and thus no building of your own skills.

Coding can be a fun hobby and a rewarding career. I urge you to take it seriously enough to get something out of your education. You can only get so far by having others do it for you, then you are going to go hungry when you boss tells you to code something and you can't.

As I said, you already had all the lines you needed: All you had to do was assemble them.

20 for (k = 0; k < counter; k = k + 1 ) {
33 if (nums[k] < avg)
28 document.write
18 }


I just find it sad when people short-change their own futures for the sake of laziness.

All the indicators are there that you could be good at this if you'd give yourself a chance. I do wish you the best of luck if you choose to learn this.
Was This Post Helpful? 1
  • +
  • -

#8 Jagst3r15  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 21-December 10

Re: Write the average and all numbers less than average

Posted 01 May 2012 - 04:44 PM

Sorry, simply getting the code was not my intention in creating this topic - I was just looking for help with my structure/logic because I barely got the first loop to work and was confused as to how a second one would be added. Apologies if this caused any problems :)

This post has been edited by Jagst3r15: 01 May 2012 - 04:57 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1