PDA

View Full Version : More JavaScript Questions


comrade
May 21, 2003, 02:20 PM
Last week some of you helped me figure out the problems in my JavaScript code, and that was greatly appreciated. Well, I have another request. I am a poor college student and have not been able to buy the "optional" JavaScript book for my class (anyone got any suggestions for one? I plan to buy ti next week when I receive my paycheck from work. I hear O'Reily is good). Anyways, due to that I am having some trouble figuring out how to code something I will have to do in a lab tomorrow.

The assignment deals with IF-THEN-ELSE blocks and DO-LOOPS. Now, while I know what these are from my texbook, I am not sure how they translate to javascript. Do they translate to javascript just as they are? Are they worded different? I will try to look up a website tonight but you guys seem to give me faster and better results. Once again, I am not trying to have you do my assignment, just point me in the right direction. I really want to learn this but it is frustrating w/o good Java info to refer to.

The assignment is located here in case you are confused on what I mean:

http://www.cs.ucf.edu/courses/cop2500/assignments/cop2500lab03.html

Thanks again MacRumors!

bousozoku
May 21, 2003, 02:54 PM
I just finished a JavaScript class at Valencia. :)

My other JavaScript book is a Visual QuickStart Guide, as are most of my "other" books.

FOR i = 0 to 9 DO
sum = sum + list[i];
ENDDO

would be:

for(i = 0; i <= 9; i++)
{
sum += list(i); /* or sum = sum + list(i); */
}

Just remember that everything before the first semicolon would be to get initial values. Between there and the second semicolon would be the limit. Finally, the third part is the increment/decrement. You can get by with various combinations but I wouldn't suggest it until you've finished the homework and submitted it. :)

Shortcuts for the four basic math functions exist: +=. -=. *=, and /= as well as %= for remainder.

comrade
May 22, 2003, 04:13 PM
Well, did the lab today, but couldn't get it to work by the end of the lab. Here is what I had with some help from the lab assistant:

This is just the first set:

<html>
<head>
<title>
COP 2500 Assignment 3
</title>
</head>

<body>

<script language="JavaScript">

var list = new Array[10];
var sum;
var i;

for(i = 0; i < 10; i++)
{
list[i]= Math.round(100 * Math.random());
}
sum = 0;

for(i = 0; i < 10; i++)
{
sum = sum + list[i];
}

document.write(" <b><center>Sum()</center></b> ");
document.write(" Sum = " + sum);





</script>


</body>
</html>

I can work on it tonight and e-mail it to him, so if you see what is wrong please point it out. Thanks for your help guys.

bousozoku: I have a few friends from Valencia. How do you like it there? What is your major?

comrade
May 22, 2003, 04:14 PM
The 2nd one:

<html>
<head>
<title>
COP 2500 Assignment 3
</title>
</head>

<body>

<script language="JavaScript">
var list1 = new Array[10];
var list2 = new Array[10];

document.write("<b><center>Compare()</center></b>");

for(i = 0; i <= 10; i++)
{
list1[i] = Math.round(100 * Math.random());
list2[i] = Math.round(100 * Math.random());
}

for(i = 0; i <= 10; i++)
{
if ( list1[i] > list2[i] )
{
document.write( list1[i] + "is greater than" + list2[i];
}
else
{
document.write( list1[i] + "is less than" + list2[i];
}
}

</script>


</body>
</html>

bousozoku
May 22, 2003, 06:33 PM
Sorry, I made a mistake in the array access by specifying parentheses. They only belong in the creation of a new array.

Change the lines where you're creating an array with:

= new Array[10];

to

= new Array(10);

It's confusing and inconsistent but that's what they decided.

Valencia is a good school with some very good and some very poor instructors. All of the curricula are meaningful and constantly being re-evaluated to be aligned with the job market.

I'm currently working on a degree in Graphic Design but also taking courses toward a degree in Computer Science for which I have an A.S. already. I'll be at UCF as soon as I can manage the credits.

comrade
May 22, 2003, 08:28 PM
Hmm, that worked with the first one but the second one still doesn't display anything. This can be so frustrating... : /

Valencia is a very popular CC. I have met a lot of people that go there. UCF's not bad and they are the only public school in FL to offer Digital Media as a major. Orlando's not a bad city either (downtown area, not the tourist part).

bousozoku
May 22, 2003, 09:08 PM
One thing to remember is that everything needs to be balanced.

In HTML, you notice that you need <> in pairs and most tags have to be matched as well.

In programming, this is also important. Parentheses, braces, and brackets all need to be paired.

You'll find that this fixes the issue in the second script.