Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
May 22nd, 2003, 01:50 AM
 UCFgirl
Junior Member

Join Date: May 2003
Posts: 2
Time spent in forums: < 1 sec
Reputation 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
May 22nd, 2003, 09:29 AM
 epl
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 09:31 AM.

#3
May 22nd, 2003, 10:24 AM
 Onslaught
/(bb|[^b]{2})/

Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 5,163
Time spent in forums: 6 Days 1 h 34 m 20 sec
Reputation Power: 792
In the future please consider a better subject title.

#4
May 22nd, 2003, 04:42 PM
 UCFgirl
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

#5
May 22nd, 2003, 06:38 PM
 md_doc 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

#6
May 22nd, 2003, 06:53 PM
 epl
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 06:57 PM.