PDA

View Full Version : Help with JavaScript please!



jacko1982
06-19-2006, 10:05 AM
Hi i am having trouble with javascript i have only just started learning it and need some help with a program i am creating. if you think u can help please let me know

pjacko@gmail.com

Twey
06-19-2006, 03:47 PM
if you think u can help please let me know

pjacko@gmail.comIf you can't be bothered to watch the thread, we can't be bothered to help you :) There's even a Subscribe to this Thread (http://www.dynamicdrive.com/forums/subscription.php?do=addsubscription&t=10513) option.

Some information about what you're actually trying to do would be nice, too.

jacko1982
06-20-2006, 11:46 AM
hi sorry i am new to this

I am trying to create a program that will ask a user for 4 different lengths and widths of a rectangle and to store the data in an array and output results

i have just started learning java script. does anybody know about arrays

Twey
06-20-2006, 02:05 PM
<html>
<head>
<title>A</title>

<style type="text/css">
.rect {
background-color: white;
border: 1px solid gray;
color: gray;
}
</style>

<script type="text/javascript">
function Rectangle(l, w, x, y) {
this.length = l;
this.width = w;
this.area = l * w;
this.x = x || Math.floor(Math.random() * 640);
this.y = y || Math.floor(Math.random() * 480);
this.dragging = false;
this.id = Rectangle.currentID++;
}

Rectangle.prototype.move = function(x, y) {
this.x = x;
this.y = y;
};

Rectangle.prototype.draw = function() {
var first = !this.element;
if(first) {
this.colour = (function() {
var light = [];
for(var i=0;i<3;i++)
light.push(Math.floor(Math.random() * 256).toString(16));
return "#" + light.join("");
})();
this.element = document.createElement("div");
this.element.rect = this;
this.element.style.backgroundColor = this.colour;
this.paragraph = document.createElement("p");
this.paragraph.style.top = "47%";
this.paragraph.style.position = "relative";
this.paragraph.style.textAlign = "center";
this.element.appendChild(this.paragraph);
this.info = document.createTextNode("");
this.paragraph.appendChild(this.info);
this.paragraph.appendChild(document.createElement("sup")).appendChild(document.createTextNode("2"));
this.element.onclick = function() {
this.rect.remove();
};
}
this.info.nodeValue = this.length + "cm x " + this.width + "cm = " + this.area + "cm";
this.element.style.position = "absolute";
this.element.style.width = this.width + "cm";
this.element.style.height = this.length + "cm";
this.element.style.top = this.y + "px";
this.element.style.left = this.x + "px";
this.element.className = "rect";
this.element.id = "rect" + this.id;
if(first) document.body.appendChild(this.element);
};

Rectangle.prototype.remove = function() {
this.paragraph.removeChild(this.info);
delete this.info;
this.element.removeChild(this.paragraph);
delete this.paragraph;
document.body.removeChild(this.element);
delete this.element;
delete this;
}

Rectangle.prototype.resize = function(l, w) {
this.length = l;
this.width = w;
this.area = l * w;
};

Rectangle.currentID = 0;

Rectangle.allRectangles = [];

Rectangle.create = function() {
var l = "", w = "";
while(isNaN(parseFloat(l))) {
if(l) window.alert(l + " is not a valid length.");
l = window.prompt("Please enter the length of rectangle in centimeters:");
}
while(isNaN(parseFloat(w))) {
if(w) window.alert(w + " is not a valid width.");
w = window.prompt("Please enter the width of rectangle in centimeters:");
}
var r = new Rectangle(l, w);
rects.push(r);
r.draw();
return r;
};

var rects = [];

</script>
</head>
<body>
<input type="button" onclick="Rectangle.create();" value="Create Rectangle">
</body>
</html>

jacko1982
06-20-2006, 02:10 PM
thank you very much. this code is a little to advance for me , is it possible i could email you some code i am working on

cheers

Twey
06-20-2006, 02:29 PM
Not at the moment, I'm afraid... my web host has just shut down (http://www.webhostingtalk.com/showthread.php?t=522913), so I currently have no email. :( I'll sort it out at some point.

serothis
06-20-2006, 08:52 PM
a good place to help the beginner learn about javascript and a dozon other web programs is http://www.w3schools.com