Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 19th, 2008, 9:17 PM   #1
scottrobot
Newbie
 
Join Date: Mar 2008
Posts: 3
Rep Power: 0 scottrobot is on a distinguished road
Help plz

 <script> /*two dimensional arrays*/

var a=[];
var ctr;
var Maths=new Array(5);
var English=new Array(5);
var Total=(Maths[ctr]+English[ctr]);

for(ctr=0; ctr<5; ctr++)
{
	a[ctr]="Hello";
	Maths[ctr]=0;
	English[ctr]=0;
	Total[ctr]=0;
}

for(ctr=0; ctr<5; ctr++)
{
	a[ctr]=prompt("Enter Name");
	Maths[ctr]=prompt("Enter Math Grade");
	English[ctr]=prompt("Enter English Grade");
}
document.write("<TABLE Border=10 ALIGN=center>");
document.write("<TR>");
document.write("<TH>Name</TH>");
document.write("<TH>Maths Marks</TH>");
document.write("<TH>English Marks</TH>");
document.write("<TH>Total</TH>");
document.write("<TH>Average</TH>");
document.write("</TR>");

for(ctr=0; ctr<5; ctr++)
{
document.write("<TR>");
document.write("<TD>" + a[ctr] + "</TD>");
document.write("<TD>" + Maths[ctr] + "</TD>");
document.write("<TD>" + English[ctr] + "</TD>");
document.write("<TD>" + (Maths[ctr] + English[ctr]) + "</TD>");
document.write("<TD>" + (Maths[ctr] + English[ctr])/2 + "</TD>");
document.write("</TR>");
}

document.write("</TABLE>");

</script>


im trying to add Maths and English to create a total .. and also to find the average of Maths and English .. obviously im doing something wrong but i dont rly remember how to do this.. this is also my first time working with arrays
scottrobot is offline   Reply With Quote
Old Mar 19th, 2008, 11:16 PM   #2
Grich
Hobbyist Programmer
 
Grich's Avatar
 
Join Date: Sep 2007
Location: Sydney - Australia
Posts: 197
Rep Power: 2 Grich is on a distinguished road
Re: Help plz

Alright, what you are doing is adding two strings together. So, 45 + 45 = 4545, you need 45 + 45 = 90. This is what you have to to.
<script> /*two dimensional arrays*/

var a=[];
var ctr;
var Maths=new Array(5);
var English=new Array(5);
var Total=(Maths[ctr]+English[ctr]);

for(ctr=0; ctr<5; ctr++)
{
	a[ctr]="Hello";
	Maths[ctr]=0;
	English[ctr]=0;
	Total[ctr]=0;
}

for(ctr=0; ctr<5; ctr++)
{
	a[ctr]=prompt("Enter Name");
	Maths[ctr]=prompt("Enter Math Grade");
	English[ctr]=prompt("Enter English Grade");
}
document.write("<TABLE Border=10 ALIGN=center>");
document.write("<TR>");
document.write("<TH>Name</TH>");
document.write("<TH>Maths Marks</TH>");
document.write("<TH>English Marks</TH>");
document.write("<TH>Total</TH>");
document.write("<TH>Average</TH>");
document.write("</TR>");

for(ctr=0; ctr<5; ctr++)
{
document.write("<TR>");
document.write("<TD>" + a[ctr] + "</TD>");
document.write("<TD>" + Maths[ctr] + "</TD>");
document.write("<TD>" + English[ctr] + "</TD>");
// This is the new code here.
document.write("<TD>" + (Number(Maths[ctr]) + Number(English[ctr])) + "</TD>");
document.write("<TD>" + (Maths[ctr] + English[ctr])/2 + "</TD>");
document.write("</TR>");
}

document.write("</TABLE>");

</script>
The Number() function turns a string into a number.
__________________
SYNTAX ERROR ...
Grich is offline   Reply With Quote
Old Mar 20th, 2008, 7:31 AM   #3
scottrobot
Newbie
 
Join Date: Mar 2008
Posts: 3
Rep Power: 0 scottrobot is on a distinguished road
Re: Help plz

thanks a lot man it works
scottrobot is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:54 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC