PDA

View Full Version : Algorithms?



mburt
09-04-2006, 09:27 PM
I've sort of heard about algorithms before and it has something to do with lots of data. But I've never really understood what it meant. Could someone tell what an algorithm is, and possibly, how to make one?

Twey
09-04-2006, 09:31 PM
An algorithm is just a way to solve a problem, especially mathematical. Thus, they take input and give output.

For example, the algorithm to find the area of a circle from its radius is d = PI * r * r, where d (diameter) is the output and r (radius) is the input. The algorithm to find the pixel top of an HTML element on the page in most browsers is (based on code from QuirksMode.org):
if (obj.offsetParent) {
curtop = obj.offsetTop;
while (obj = obj.offsetParent)
curtop += obj.offsetTop;
}... where obj (object whose top to get) is the input and curtop (top in pixels of obj) is the output.

mburt
09-04-2006, 10:11 PM
For some reason, I knew you would answer this. You talk about algorithms like ice-cream. :)

mburt
09-04-2006, 10:15 PM
So what's the real point to algorithms?

blm126
09-04-2006, 11:26 PM
To solve problems

Twey
09-04-2006, 11:51 PM
You talk about algorithms like ice-cream.Lol! What's that supposed to mean? :p
[The point of algorithms is] To solve problemsPrecisely. :)

Every time you write a script or program, the first thing you do -- consciously or not -- is to design an algorithm, a simple breakdown of the exact steps the computer needs to perform to accomplish the task. In fact, in some ways it could be said that a computer program is an algorithm.

mburt
09-05-2006, 12:00 AM
Lol! What's that supposed to mean?

It was a bad simile.... :p

shachi
09-05-2006, 05:05 AM
This may be the algorithm to find the bottom left pixel of an object.;)



if (obj.offsetParent) {
curtop = obj.offsetTop;
while (obj = obj.offsetParent)
curtop += obj.offsetTop+obj.offsetWidth;
}

Twey
09-05-2006, 04:30 PM
I'm afraid it isn't. :p

The algorithm above only expressed one dimension; to express two, you'd need an array or an object, which is where my getBoundingBox function comes in:
function getBoundingBox(obj) {
var curleft = 0,
oObj = obj;
if (obj.offsetParent)
while (obj.offsetParent) {
curleft += obj.offsetLeft;
obj = obj.offsetParent;
}
else if (obj.x) curleft += obj.x;

obj = oObj;

var curtop = 0;
if (obj.offsetParent)
while (obj.offsetParent) {
curtop += obj.offsetTop;
obj = obj.offsetParent;
}
else if (obj.y) curtop += obj.y;

var curright = curleft + oObj.offsetWidth,
curbottom = curtop + oObj.offsetHeight;

return {'left' : curleft, 'top' : curtop, 'right' : curleft + oObj.offsetWidth, 'bottom' : curtop + oObj.offsetHeight};
}

shachi
09-05-2006, 04:33 PM
Great function Twey but it would be useful to me if you could tell me how to use it.;)

And I thought Algorithms were all about solving a problem step wise something like this:



if two plus three equals 5
show the user 5
else
simply show him you are wrong.

Twey
09-05-2006, 04:59 PM
That is step-wise :) So are all computer programs.
Great function Twey but it would be useful to me if you could tell me how to use it.
var box = getBoundingBox(element);
var bottom = box.bottom,
left = box.left;

shachi
09-05-2006, 05:22 PM
Oh, thanks Twey for the script. By the way will this work too??



var box = new getBoundingBox(somelement);
var bottom = box.bottom;
...


And by the way what is the difference between using var to declare a variable and not using it??

blm126
09-05-2006, 10:34 PM
And by the way what is the difference between using var to declare a variable and not using it??
the scope of the variable. For example


function 1(){
somevar = "some random text";
}
function 2(){
document.write(somevar);
}

will work. but


function 1(){
var somevar = "some random text";
}
function 2(){
document.write(somevar);
}

will not

Twey
09-05-2006, 10:41 PM
Not quite. var declares a variable. If you don't use the var keyword, it's assumed you're setting a property of the global object, which is window. Thus,
foo = 3;is equivalent to
window.foo = 3;

blm126
09-05-2006, 10:45 PM
...of the global object, which is window.
The global object doesn't have to be window though does it? That is just the case for web browsers.

Twey
09-06-2006, 12:45 AM
Yes, sorry. :)

mwinter
09-06-2006, 06:29 PM
... what is the difference between using var to declare a variable and not using it??

the scope of the variable.

More importantly, though, not declaring global variables can lead to an exception in IE should the identifier conflict with an id attribute value.

For example:



<ul id="menu">
<!-- ... -->
</ul>



menu = document.getElementById('menu');






function 1(){
somevar = "some random text";
}
function 2(){
document.write(somevar);
}

will work.

If the function identifiers were acceptable, yes. :p

Mike