The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Software Design
|
Please Help-School Assignment
Discuss Please Help-School Assignment in the Software Design forum on Dev Shed. Please Help-School Assignment Software design forum discussing design principles and non-language specific algorithms. Get help with logic, algebraic, or relational concepts.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 22nd, 2003, 12:50 AM
|
|
Junior Member
|
|
Join Date: May 2003
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Please Help-School Assignment
Hello, I have an assignment for school and I cannot figure out how to do it right. I am converting an algorithm into javascript. I am supposed to have a list of numbers, but I can only figure out how to do 1 number. This is my assignment, and below that is the code I have written. PLEASE HELP!!
Assignment:
Convert the following algorithm into JavaScript code within your HTML document. Your final web page should display the algorithm title, the input and the output.
Input:
Integerlist[10];
OTHER VARIABLES:
Integer sum;
Integer i;
INITIALIZATION:
FOR i = 0 to 9 DO
list[i] = random integer between 0 and 100;
ENDIF
sum = 0;
COMPUTATION:
FOR i = 0 to 9 DO
sum = sum + list[i];
ENDDO
OUTPUT:
return(sum);
And the code i wrote is:
<html>
<head>
<title>Sum()
</title>
</head>
<body>
<h1>Sum()</h1> <br><br>
<script language="JavaScript">
var list;
var sum;
var i;
i= [1, 2, 3, 4, 5, 6, 7, 8, 9]
list = Math.round(100 * Math.random())
for (i = 0; i < 10; i++)
{
list = Math.round(100 * Math.random())
}
sum = 0
for (i = 0; i < 10; i++)
{
sum = sum + list
}
document.write("the list is=" + list + "<br />")
document.write("the sum is=" + sum)
</script>
</body>
</html>
what am I doing wrong?
Thanks SO much!!!
|

May 22nd, 2003, 08:29 AM
|
|
Contributing User
|
|
Join Date: Mar 2001
Location: Dublin
Posts: 413
Time spent in forums: 2 h 18 m 18 sec
Reputation Power: 13
|
|
try this:
Code:
var list=new Array(10);// not sure about initialising this like this... maybe be unecessary or wrong
var sum=0;
var i;
//generate list[] inputs
for (i=0; i<10; i++)
{list[i]=Math.round(100 * Math.random());}
//iterate over list[], building up the running total
for (i = 0; i < 10; i++)
{sum+=list[i];}
//output
document.write("the list is: [");
for (i = 0; i < 10; i++)
{document.write(list[i]);
if (i<9) {document.write(", ");}
}
document.write("]<br>the sum is: " + sum)
Last edited by epl : May 22nd, 2003 at 08:31 AM.
|

May 22nd, 2003, 09:24 AM
|
 |
/(bb|[^b]{2})/
|
|
Join Date: Nov 2001
Location: Somewhere in the great unknown
|
|
In the future please consider a better subject title.
See this thread for more info.
|

May 22nd, 2003, 03:42 PM
|
|
Junior Member
|
|
Join Date: May 2003
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Hey thanks, I got it figured out!!! Big Help!!!
-Melissa
|

May 22nd, 2003, 05:38 PM
|
|
Guest
|
|
Posts: n/a
Time spent in forums:
Reputation Power:
|
|
Quote: Originally posted by UCFgirl
Hey thanks, I got it figured out!!! Big Help!!!
-Melissa |
I cannot help but wonder if that means you helped her figure it out or the cut and paste of your code into her editor of choice worked 
|

May 22nd, 2003, 05:53 PM
|
|
Contributing User
|
|
Join Date: Mar 2001
Location: Dublin
Posts: 413
Time spent in forums: 2 h 18 m 18 sec
Reputation Power: 13
|
|
|
funny alright - i was a bit taken aback alright when i read it
well done figuring it out melissa - i suppose, to your credit, i did begin with a copy and paste of your code in the first place
good luck with your assignment (make sure to pass this link around to the rest of your class)
Last edited by epl : May 22nd, 2003 at 05:57 PM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|