0

Expanding code I've been working on for a supplement tracker but my current function is not returning accurate count of numbers greater than the average 'mean' nor the count of integers below the mean average. I've also commented out two questions within the code because I don't quite understand why the array is set to index[0]. I've learned much from the comments and searching for answers here. So thankful this site exists! Looking to learn a bit more hopefully with this question.

function suppArray() {
var nums = new Array(); //create array 
var sum = 0; //variable to hold sum of integers in array
var avg = 0; //variable to hold the average
var i; 
var count = 0;
var count2 = 0;
var contents = ''; //variable to hold contents for output 

    var dataPrompt = prompt("How many numbers do you want to enter?", "");
    dataPrompt = parseInt(dataPrompt);

    for(i = 0; i <= dataPrompt - 1; i++) { //loop to fill the array with numbers 
        nums[i] = prompt("Enter a number","");
        nums[i] = parseInt(nums[i]);
        contents += nums[i] + " "; //variable that will be called to display contents
        sum = sum + nums[i];
    }
        avg = sum / nums.length; 
    for(i = 0; i < nums.length; i++) { //loop to find the largest number
        var biggest = nums[0]; //why does this have to be index 0 and not 'i' ?
        if(nums[i] > biggest) 
        biggest = nums[i]; //largest integer in array
    }
    for(i = 0; i < nums.length; i++) { //loop to find smallest integer
        var smallest = nums[0]; //why does this have to be the index 0 and not 'i' ??
        if(nums[i] < smallest) 
        smallest = nums[i]; //smallest integer in array
    }       
    for(count = 0; count < nums.length; count++) { //count of numbers higher than average
        if(nums[i] > avg) 
        count = nums[i];
    }   
    for(count2 = 0; count2 < nums.length; count2++) { //count of numbers lower than average
        if(nums[i] < avg)
        count2 = nums[i];
    }
}
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
allendks45
  • 339
  • 5
  • 18

1 Answers1

2

Your function isn't returning the right values because you are assigning count or count2 inccorectly. If you run through your code at the end count and count2 will be equal to nums.length. This is because you are using them in the for loop. As well in the loops you reference i which is (I believe) also equal to nums.length at this point.

I think you want something like this:

count = 0;
count2 = 0;

for(i = 0; i < nums.length; i++) 
{
    if(nums[i] > avg)
    {
        count++; //Increase the count of numbers above the average
    }
    else if(nums[i] < avg)
    {
        count2++;  //Increase the count of numbers below the average
    }
}   

You may want to do some reading on scope and the for loop as you seem a little confused on them.

Edit

If you want the biggest and smallest values in the array you can do something like this:

//Assign the values to the first element by default
var biggest = nums[0];
var smallest = nums[0];

for(var i = 1; i < nums.length; i++)
{
    //Set biggest to the larger number, either biggest or the current number
    biggest = Math.max(biggest, nums[i]);
    //Set smallest to the smaller number, either biggest or the current number
    smallest = Math.min(smallest, nums[i]);
}

Note: This assumes you have at least 1 value in the array

Community
  • 1
  • 1
amura.cxg
  • 2,348
  • 3
  • 21
  • 44
  • Initially I was attempting to code it using just 'for' loop but did come across the Math.max method while searching here. This is the most efficient way to achieve the correct output. Also, overlooked the fact the counter should be updated to hold the count which works too. Appreciate the comments. For loops and nested loops are giving me fits...hard to follow and truly understand them. – allendks45 Apr 01 '15 at 15:05
  • For loops and nested loops take a bit of getting used to. One of the things I find handy to do is run through your code on paper (making sure you only do what is written in the code) with some small input. That usually helps find mistakes – amura.cxg Apr 01 '15 at 15:40