You are viewing an archived version of the site which is no longer maintained.
Go to the current live site or the Adventure Gamers forums
Adventure Gamers

Home Adventure Forums Misc. Chit Chat Super Huge Homework Help!


 
 
LinkBack Thread Tools
Old 04-04-2006, 10:44 PM   #1
Banned User
 
SakSquash's Avatar
 
Join Date: Nov 2004
Location: New Paltz...for now...
Posts: 6,177
Default Super Huge Homework Help!

Ok, now we're learning java script, but the dude assigns us homework about stuff we haven't learned! He teaches us the basics, then assigns us some advanced script that we have to make and I have noooooo clue! Anyway, here's what it says:



Please help! It's due tomorrow/today!
SakSquash is offline  
Old 04-04-2006, 10:51 PM   #2
Friendly Server Admin
 
tabacco's Avatar
 
Join Date: Sep 2003
Location: Marin County, CA
Posts: 4,087
Default

Do you know how to do for() loops?
tabacco is offline  
Old 04-04-2006, 11:04 PM   #3
Banned User
 
SakSquash's Avatar
 
Join Date: Nov 2004
Location: New Paltz...for now...
Posts: 6,177
Default

Huh?
SakSquash is offline  
Old 04-04-2006, 11:15 PM   #4
Friendly Server Admin
 
tabacco's Avatar
 
Join Date: Sep 2003
Location: Marin County, CA
Posts: 4,087
Default

Quote:
Originally Posted by SakSquash
Huh?
Loops of any kind? What about document.write()? Your question refers to some reading... is it possible these things are in there?
tabacco is offline  
Old 04-04-2006, 11:17 PM   #5
Banned User
 
SakSquash's Avatar
 
Join Date: Nov 2004
Location: New Paltz...for now...
Posts: 6,177
Default

document.write I know, as well as document.writeln, but no clue about looping. There is some stuff in the book about it, but nothing that has to do with "squares and cubes." Trust me, i've looked all through the book.
SakSquash is offline  
Old 04-04-2006, 11:19 PM   #6
Friendly Server Admin
 
tabacco's Avatar
 
Join Date: Sep 2003
Location: Marin County, CA
Posts: 4,087
Default

Quote:
Originally Posted by SakSquash
document.write I know, as well as document.writeln, but no clue about looping. There is some stuff in the book about it, but nothing that has to do with "squares and cubes." Trust me, i've looked all through the book.
That's all you need to know, then. squares and cubes are simple math. The square of a number is the number times itself, and the cube is the square times the number again.

So, let's say you have a variable:
var number = 0;

If you were to make a loop that executed ten times (check out for loops), and if you added one to the number each time, it'd be pretty simple to write out the squares and cubes.

(To do powers in javascript, by the way, use Math.pow(number,power)
tabacco is offline  
Old 04-04-2006, 11:23 PM   #7
Banned User
 
SakSquash's Avatar
 
Join Date: Nov 2004
Location: New Paltz...for now...
Posts: 6,177
Default

What you said right there? All greek.
SakSquash is offline  
Old 04-04-2006, 11:33 PM   #8
Banned User
 
SakSquash's Avatar
 
Join Date: Nov 2004
Location: New Paltz...for now...
Posts: 6,177
Default

Can't you just do it for me? It's the only way i'm going to learn this.
SakSquash is offline  
Old 04-04-2006, 11:33 PM   #9
Homer of Kittens
 
SoccerDude28's Avatar
 
Join Date: Aug 2004
Location: San Francisco, Bay Area
Posts: 4,374
Default

Here you go. Runs on both mozilla firefox and IE:

Copy and paste this into some html document.

<HTML>
<HEAD>
<script language="javascript">
function calcCubes()
{
var tTable = document.getElementById("cubes");

var tRow = tTable.insertRow(tTable.rows.length);
var tHeader1 = tRow.insertCell(tRow.cells.length);
tHeader1.innerHTML="number";
var tHeader2 = tRow.insertCell(tRow.cells.length);
tHeader2.innerHTML="square";
var tHeader3 = tRow.insertCell(tRow.cells.length);
tHeader3.innerHTML="cube";

var i = 0;
for(i=0; i<=10; i++)
{
var square = i*i;
var cube = i*i*i;
var tRow = tTable.insertRow(tTable.rows.length);
var tCell1 = tRow.insertCell(tRow.cells.length);
tCell1.innerHTML = i;
var tCell2 = tRow.insertCell(tRow.cells.length);
tCell2.innerHTML = square;
var tCell3 = tRow.insertCell(tRow.cells.length);
tCell3.innerHTML = cube;
}
}

</script>

<body onload="calcCubes();">
<table id="cubes">
</table>
</body>
</HTML>
__________________
--------------------------------------------------
Games I am playing: Jeanne D'Ark (PSP)

Firefox rules
SoccerDude28 is offline  
Old 04-04-2006, 11:35 PM   #10
Banned User
 
SakSquash's Avatar
 
Join Date: Nov 2004
Location: New Paltz...for now...
Posts: 6,177
Default

It has to be in a table.
SakSquash is offline  
Old 04-04-2006, 11:36 PM   #11
Homer of Kittens
 
SoccerDude28's Avatar
 
Join Date: Aug 2004
Location: San Francisco, Bay Area
Posts: 4,374
Default

Quote:
Originally Posted by SakSquash
It has to be in a table.
It is in a table. Copy and paste it into an html document and see for yourself.
__________________
--------------------------------------------------
Games I am playing: Jeanne D'Ark (PSP)

Firefox rules
SoccerDude28 is offline  
Old 04-04-2006, 11:37 PM   #12
Banned User
 
SakSquash's Avatar
 
Join Date: Nov 2004
Location: New Paltz...for now...
Posts: 6,177
Default

Actually, on second thought, I can't use any of that. We haven't learned anything like that, and he'll know I didn't do it. I just wont hand it in. **** it!!
SakSquash is offline  
Old 04-04-2006, 11:37 PM   #13
Friendly Server Admin
 
tabacco's Avatar
 
Join Date: Sep 2003
Location: Marin County, CA
Posts: 4,087
Default

Quote:
Originally Posted by SakSquash
What you said right there? All greek.
Okay, first the basic math...

The square of a number is that number times itself. So, the square of 2 is 4, the square of 6 is 36, the square of 10 is 100. Fair enough?

The cube is calculated just like the square, only you multiple by the same number once more. The cube of 2 is 8, or (2 * 2 * 2), the cube of 10 is 1000, or (10 * 10 * 10), etc.

Now onto the code...

Basically, all you need to do is count from 0 to 10, and print the square and cube of each number along the way, which is pretty much how you'd probablky do it on paper, right? So what you do is say "Ah, zero... well, the square of zero is zero, and the cube of zero is also zero" then you write that down, and move on to 1, and so on until you hit 10 and the homework's done, right?

Well, you just need to do the exact same thing with code. It just requires you to think a little more closely about the process than you woudl if you were doing it on paper.

Check out the for loop:
Code:
for(var number = 0; number <= 10; number = number + 1) { 
   foo
}
So as you can see, there are three parts to the loop statement, seperated by semicolons. The first one declares a variable called number, and sets the initial value to 0. Simple enough.

The second part is called the condition. Each time the loop runs, it will do whatever is where "foo" is in the code. It will keep doing "foo" over and over as long as the condition in the second part of the loop statement is true. So here it will keep looping as long as number is less than or equal to 10.

The third part is where the real magic happens. Each time the loop is run, this third part will be executed. So, after each time the loop runs here, number is going to be increased by one. That means that on the first run-through, number equals 0. The second time, it equals 1, and so on. Now, eventually it's going to get set to 11, at which point the second part will kick in and stop the loop from running again with 11 as the value.

So now you've got the code to count from 0-10 for you, right? That means all you have to do now is replace "foo" with the code to print those squares and cubes. To take the square of a number, you're going to use
Code:
Math.pow(number,2);
and to take the cube you're going to use
Code:
Math.pow(number,3);
Math.pow() is a function built into Javascript to take the power of a number (with the square being the second power, and a cube being the third). Basically the second argument to Math.pow() is the number of times the first argument should be multiplied by itself.

Does this help at all?
tabacco is offline  
Old 04-04-2006, 11:37 PM   #14
Banned User
 
SakSquash's Avatar
 
Join Date: Nov 2004
Location: New Paltz...for now...
Posts: 6,177
Default

Quote:
Originally Posted by SoccerDude28
It is in a table. Copy and paste it into an html document and see for yourself.
I did, and there's no table. They're just numbers without borders.
SakSquash is offline  
Old 04-04-2006, 11:39 PM   #15
Banned User
 
SakSquash's Avatar
 
Join Date: Nov 2004
Location: New Paltz...for now...
Posts: 6,177
Default

Quote:
Originally Posted by tabacco
Okay, first the basic math...

The square of a number is that number times itself. So, the square of 2 is 4, the square of 6 is 36, the square of 10 is 100. Fair enough?

The cube is calculated just like the square, only you multiple by the same number once more. The cube of 2 is 8, or (2 * 2 * 2), the cube of 10 is 1000, or (10 * 10 * 10), etc.

Now onto the code...

Basically, all you need to do is count from 0 to 10, and print the square and cube of each number along the way, which is pretty much how you'd probablky do it on paper, right? So what you do is say "Ah, zero... well, the square of zero is zero, and the cube of zero is also zero" then you write that down, and move on to 1, and so on until you hit 10 and the homework's done, right?

Well, you just need to do the exact same thing with code. It just requires you to think a little more closely about the process than you woudl if you were doing it on paper.

Check out the for loop:
Code:
for(var number = 0; number <= 10; number = number + 1) { 
   foo
}
So as you can see, there are three parts to the loop statement, seperated by semicolons. The first one declares a variable called number, and sets the initial value to 0. Simple enough.

The second part is called the condition. Each time the loop runs, it will do whatever is where "foo" is in the code. It will keep doing "foo" over and over as long as the condition in the second part of the loop statement is true. So here it will keep looping as long as number is less than or equal to 10.

The third part is where the real magic happens. Each time the loop is run, this third part will be executed. So, after each time the loop runs here, number is going to be increased by one. That means that on the first run-through, number equals 0. The second time, it equals 1, and so on. Now, eventually it's going to get set to 11, at which point the second part will kick in and stop the loop from running again with 11 as the value.

So now you've got the code to count from 0-10 for you, right? That means all you have to do now is replace "foo" with the code to print those squares and cubes. To take the square of a number, you're going to use
Code:
Math.pow(number,2);
and to take the cube you're going to use
Code:
Math.pow(number,3);
Math.pow() is a function built into Javascript to take the power of a number (with the square being the second power, and a cube being the third). Basically the second argument to Math.pow() is the number of times the first argument should be multiplied by itself.

Does this help at all?
No, he didn't teach us any of this. I'll show you what he did teach us:

Quote:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<?xml version = "1.0"?><!-- Fig. 7.8: addition.html --><!-- Addition Program --><HTML
xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>An Addition Program</TITLE>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<SCRIPT type=text/javascript>
<!--
var firstNumber, // first string entered by user
secondNumber, // second string entered by user
number1, // first number to add
number2, // second number to add
sum; // sum of number1 and number2


// read in first number from user as a string
firstNumber =
window.prompt( "Enter first integer", "0" );

// read in second number from user as a string
secondNumber =
window.prompt( "Enter second integer", "0" );

// convert numbers from strings to integers
number1 = parseInt( firstNumber );
number2 = parseInt( secondNumber );

// add the numbers
sum = number1 + number2;

// display the results
document.writeln( "<h1>The sum is " + sum + "</h1>" );
// -->
</SCRIPT>

<META content="MSHTML 6.00.2900.2802" name=GENERATOR></HEAD>
<BODY>
<P>Click Refresh (or Reload) to run the script again</P><!--
************************************************** ************************
* (C) Copyright 1992-2004 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
************************************************** ************************
--></BODY></HTML>
That's an addition program, and that's the most advanced thing he has taught us.
SakSquash is offline  
Old 04-04-2006, 11:40 PM   #16
Friendly Server Admin
 
tabacco's Avatar
 
Join Date: Sep 2003
Location: Marin County, CA
Posts: 4,087
Default

Quote:
Originally Posted by SakSquash
Can't you just do it for me? It's the only way i'm going to learn this.
I'm not just going to do it for you, that won't actually teach anything. If I just did it for you, you'd know how to print squares and cubes, but it wouldn't actually help teach you Javascript.

Although Soccerdude did it, so I guess that pretty much makes it a moot point
tabacco is offline  
Old 04-04-2006, 11:41 PM   #17
Banned User
 
SakSquash's Avatar
 
Join Date: Nov 2004
Location: New Paltz...for now...
Posts: 6,177
Default

What Soccerdude did didn't help at all, because I haven't seen any of that stuff before.
SakSquash is offline  
Old 04-04-2006, 11:41 PM   #18
Homer of Kittens
 
SoccerDude28's Avatar
 
Join Date: Aug 2004
Location: San Francisco, Bay Area
Posts: 4,374
Default

Quote:
Originally Posted by SakSquash
I did, and there's no table. They're just numbers without borders.
By default tables don't have borders in HTML. To change this very easily, go to the line that says:
<table id="cubes">

and change it to
<table border=1 id="cubes"> (add border=1 meaning that the table has a border of width 1).
__________________
--------------------------------------------------
Games I am playing: Jeanne D'Ark (PSP)

Firefox rules
SoccerDude28 is offline  
Old 04-04-2006, 11:42 PM   #19
Friendly Server Admin
 
tabacco's Avatar
 
Join Date: Sep 2003
Location: Marin County, CA
Posts: 4,087
Default

Quote:
Originally Posted by SakSquash
No, he didn't teach us any of this. I'll show you what he did teach us:

That's an addition program, and that's the most advanced thing he has taught us.
Well, read over what I just wrote and see if it helps at all. I'm more than happy to answer questions and stuff, but I don't want to just deliver finished code.
tabacco is offline  
Old 04-04-2006, 11:42 PM   #20
Homer of Kittens
 
SoccerDude28's Avatar
 
Join Date: Aug 2004
Location: San Francisco, Bay Area
Posts: 4,374
Default

Quote:
Originally Posted by SakSquash
What Soccerdude did didn't help at all, because I haven't seen any of that stuff before.
What have you seen, maybe if you tell us we can help you more. How can you do a table in javascript without seeing what I showed you???

Edit: ahhhhhhh okay use document.writeln then and whatever tabsie taught you
__________________
--------------------------------------------------
Games I am playing: Jeanne D'Ark (PSP)

Firefox rules
SoccerDude28 is offline  
 



Thread Tools

 


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.