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?
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?
- Mike
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):... where obj (object whose top to get) is the input and curtop (top in pixels of obj) is the output.Code:if (obj.offsetParent) { curtop = obj.offsetTop; while (obj = obj.offsetParent) curtop += obj.offsetTop; }
Last edited by Twey; 09-04-2006 at 09:36 PM.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
For some reason, I knew you would answer this. You talk about algorithms like ice-cream.![]()
- Mike
So what's the real point to algorithms?
- Mike
To solve problems
Lol! What's that supposed to mean?Originally Posted by mburt
Precisely.Originally Posted by blm126
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.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
It was a bad simile....Lol! What's that supposed to mean?![]()
- Mike
This may be the algorithm to find the bottom left pixel of an object.
Code:if (obj.offsetParent) { curtop = obj.offsetTop; while (obj = obj.offsetParent) curtop += obj.offsetTop+obj.offsetWidth; }
I'm afraid it isn't.
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:Code: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}; }
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
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:
Code:if two plus three equals 5 show the user 5 else simply show him you are wrong.
Last edited by shachi; 09-05-2006 at 04:38 PM.
Bookmarks