Link to home
Start Free TrialLog in
Avatar of buzzy333
buzzy333

asked on

Javascript - array question - Basic

Hi

Basic question but just can't get my head round it.

This javascript program details the maximum no of visitors. But there are two months with the same ammount of visitors. How can I get it to output both the months and the number of visitors?


var maximumVisitors; 
var visitorsArray = [88,62,200,100,200,34,123,34,76,199,145,11] ;
var monthNamesArray = ['January','February','March','April','May','June','July','August','September','October','November','December']; 

maximumVisitors = 0;

for (var month = 1; month < visitorsArray.length; month = month + 1) 
{
    if (visitorsArray[month] > visitorsArray[maximumVisitors]) 
    {
        maximumVisitors = month;
    }
}
document.write('Maximum Monthly Visitors of  ' 
                + visitorsArray[maximumVisitors] + ' was in ' 
                + monthNamesArray[maximumVisitors]);

Open in new window

Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

make it

var maximumVisitors = new Array();
var visitorsArray = [88,62,200,100,200,34,123,34,76,199,145,11] ;
var monthNamesArray = ['January','February','March','April','May','June','July','August','September','October','November','December'];

maximumVisitors = 0;

for (var month = 1; month < visitorsArray.length; month = month + 1)
{
    if (visitorsArray[month] > visitorsArray[maximumVisitors])
    {
        maximumVisitors[maximumVisitors.length] = month;
    }
}
for ( var counter = 0; counter < maximumVisitors.length; counter++)
document.write('Maximum Monthly Visitors of  '
                + visitorsArray[maximumVisitors] + ' was in '
                + monthNamesArray[maximumVisitors]);
First time through the loop, keep track of the NUMBER of maximum visitors, not the month.

Second time through the loop, print any month that actually had that number.

var maximumVisitors; 
var visitorsArray = [88,62,200,100,200,34,123,34,76,199,145,11] ;
var monthNamesArray = ['January','February','March','April','May','June','July','August','September','October','November','December']; 

maximumVisitors = 0;

for (var month = 1; month < visitorsArray.length; month = month + 1) 
{
    if (visitorsArray[month] > maximumVisitors) 
    {
        maximumVisitors = visitorsArray[month];
    }
}

for (var month = 1; month < visitorsArray.length; month = month + 1) 
{
    if (visitorsArray[month] == maximumVisitors)
    {
        document.write('Maximum Monthly Visitors of  ' 
                + visitorsArray[month] + ' was in ' 
                + monthNamesArray[month]);
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Göran Andersson
Göran Andersson
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of buzzy333
buzzy333

ASKER

Thanks for your help

sjklein42 output produced 'Maximum Monthly Visitors of 200 was in MarchMaximum Monthly Visitors of 200 was in May'

How would I get it it to produce the output as follows: ' Maximum monthly Visitors of 200 was in March & May'

Thanks in advance  
This should do it:

var maximumVisitors; 
var visitorsArray = [88,62,200,100,200,34,123,34,76,199,145,11] ;
var monthNamesArray = ['January','February','March','April','May','June','July','August','September','October','November','December']; 

maximumVisitors = 0;

for (var month = 1; month < visitorsArray.length; month = month + 1) 
{
    if (visitorsArray[month] > maximumVisitors) 
    {
        maximumVisitors = visitorsArray[month];
    }
}

var count = 0;
for (var month = 1; month < visitorsArray.length; month = month + 1) 
{
    if (visitorsArray[month] == maximumVisitors)
    {
	if ( ! (count++) )
	{
	        document.write('Maximum Monthly Visitors of  ' 
		        + visitorsArray[month] + ' was in ' 
			+ monthNamesArray[month]);
	}
	else
	{
	        document.write(' & ' + monthNamesArray[month]);
	}
    }
}

Open in new window

That change was easily made in the code that I suggested. Simply change the string ", " into " & ":
var visitorsArray = [88,62,200,100,200,34,123,34,76,199,145,11];
var monthNamesArray = ['January','February','March','April','May','June','July','August','September','October','November','December']; 

var max = 0;
for (var i = 1; i < visitorsArray.length; i++) {
  max = Math.max(max, visitorsArray[i]);
}

var months = [];
for (var i = 1; i < visitorsArray.length; i++) {
  if (visitorsArray[i] == max) months.push(monthNamesArray[i]);
}

document.write('Maximum Monthly Visitors of  ' 
                + max + ' was in ' 
                + months.join(' & ');

Open in new window

Hi guys.

for the question posed. I cannot use any other variables and command and should use the following structured code:

For each day
      If the vistors  that month equals the maximum
            Write out ‘the greatest monthly visitors was on ‘
       Write out abbreviated name of the month followed by a space
      End if
End for.

Cheers

guys
Do you really mean "For each day" ?  I'm guessing you meant "For each month".

This should do it:

var maximumVisitors; 
var visitorsArray = [88,62,200,100,200,34,123,34,76,199,145,11] ;
var monthNamesArray = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']; 

maximumVisitors = 0;

for (var month = 0; month < visitorsArray.length-1; month++) 
{
    if (visitorsArray[month] > maximumVisitors) 
    {
        maximumVisitors = visitorsArray[month];
    }
}

for (var month = 0; month < visitorsArray.length-1; month++) 
{
    if (visitorsArray[month] == maximumVisitors)
    {
        document.write("the greatest monthly visitors was on '  + monthNamesArray[month] + ' ');
    }
}

Open in new window

I cannot use any other variables and command and should use the following structured code

Why? Is this a homework assignment?