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:
http://img.photobucket.com/albums/v3...cars/hwork.jpg Please help! It's due tomorrow/today! |
Do you know how to do for() loops?
|
Huh?
|
Quote:
|
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.
|
Quote:
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) |
What you said right there? All greek.
|
Can't you just do it for me? It's the only way i'm going to learn this.
|
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> |
It has to be in a table.
|
Quote:
|
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!!
|
Quote:
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) { 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); Code:
Math.pow(number,3); Does this help at all? |
Quote:
|
Quote:
Quote:
|
Quote:
Although Soccerdude did it, so I guess that pretty much makes it a moot point :( |
What Soccerdude did didn't help at all, because I haven't seen any of that stuff before.
|
Quote:
<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). |
Quote:
|
Quote:
Edit: ahhhhhhh :) okay use document.writeln then and whatever tabsie taught you :) |
All times are GMT -8. The time now is 11:06 PM. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Design & Logo Copyright ©1998 - 2017, Adventure Gamers®.
All posts by users and Adventure Gamers staff members are property of their original author and don't necessarily represent the opinion or editorial stance of Adventure Gamers.