1. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
May 2003
Posts
2
Rep Power
0

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>
<title>Sum()
</title>
<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!!!
2. No Profile Picture
epl
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2001
Location
Dublin
Posts
413
Rep Power
14
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 09:31 AM.
3. In the future please consider a better subject title.
4. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
May 2003
Posts
2
Rep Power
0
Hey thanks, I got it figured out!!! Big Help!!!
-Melissa
5. No Profile Picture
md_doc
Guest
Devshed Newbie (0 - 499 posts)
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
6. No Profile Picture
epl
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2001
Location
Dublin
Posts
413
Rep Power
14
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 06:57 PM.