Jump to content

How to write out a line giving the maximum distance, and the days from two arrays?

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Lajon

Lajon

    Newbie

  • Members
  • Pip
  • 1 posts
I have stuck with my assignemnt.
I need to write out maximum distance and the days on which it was run, but I don't know how to do it using arrays for two different weeks and display one day with the distance from var weekBeginning = '12 July 2010' and two days with the distance from var weekBeginning = '19 July 2010'.
Any help appreciated.
<HTML>

<HEAD>

<TITLE>

Jogging Summary

</TITLE>

<LINK REL="stylesheet" TYPE="text/css" HREF="tma03.css">

<SCRIPT LANGUAGE = "JavaScript">

 /* 

  * M150 TMA03 2010J Q3.

  * Program to analyse the distances run by a jogger each day over the course of a week.

  */


//days of the week

var dayArray = 

['Mon', 'Tue', 'Wed', 'Thu', 'Fri','Sat', 'Sun'];

//date of a sample week

var weekBeginning = '12 July 2010'

//distance in kilometres run each day in the sample week

var distanceArray = [2,4,2,1,5,4,3];


//second sample week 

//Part (v) uncomment the next two lines.

weekBeginning = '19 July 2010'

distanceArray = [2,6,3,1,6,4,2];


//Part (i)

//Add code to display a heading, then a blank line, 

document.write('Summary for the week begining 12 July 2010' + '<BR>' + '<BR>');

// then the information for each of the seven days.

for (var day = 1; day < dayArray.length; day = day + 1)

{

	document.write(dayArray[day] + ' : ' + distanceArray[day] + ' km' + '<BR>' + '<BR>');

}

//Variable to hold the maximum value in distance array.

var maximumDistance;

maximumDistance = 0;

//Part (ii)

//Add code to find the maximum distance.

for (var distance = 1; distance < distanceArray.length; distance = distance + 1)

{

	if (distanceArray[distance] > distanceArray[maximumDistance])

	{

	maximumDistance = distance;

	}

}

//Part (iv)

//Add code that will write out the maximum distance and the days it was run on in a suitable message.

document.write('The maximum distance was ' + distanceArray[maximumDistance] 

				+' km on ' + dayArray[maximumDistance] + '.' + '<BR>');

				

</SCRIPT>


</HEAD>

<BODY>

</BODY>

</HTML>

Attached Files



#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
This belongs in Javascript, not Java.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users