Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Javascript - array question - Basic

Posted on 2011-02-23
11
Medium Priority
?
164 Views
Last Modified: 2013-10-05
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

0
Comment
Question by:buzzy333
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
11 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 34963161
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]);
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34963178
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

0
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 2000 total points
ID: 34963226
First find the highest value, the get all months that have that value:
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

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:buzzy333
ID: 34972636
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  
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34972753
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

0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 34990490
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

0
 

Author Comment

by:buzzy333
ID: 35000575
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
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35000881
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

0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 35013297
I cannot use any other variables and command and should use the following structured code

Why? Is this a homework assignment?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Find out what you should include to make the best professional email signature for your organization.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question