Jump to content

making a function generic


ctoz

Recommended Posts

I have this function — some here will recognise their input — which twice looks at a collection of elements and each time does something different with them, depending on a particular character in the ids:

function dot0() {for (i=0;i<document.getElementsByTagName("span").length; i++)  {if (document.getElementsByTagName("span").item(i).id.charAt(6) == "1") { document.getElementsByTagName("span").item(i).style.display = "inline" }if (document.getElementsByTagName("span").item(i).id.charAt(0) == "b") {document.getElementsByTagName("span").item(i).style.visibility = "hidden" }}}

I need to use variations of this twelve times on the page, so it's worth trying to make it generic. You can see the variations operating here, with its linked "/engine.js"where they progressively show and hide the little red dots to the right of the red lines (yup, it's obscure... :) ) I can describe in English, but getting it into script may be beyond my current knowledge level. There's an attempt below.In the first "if" statement, it's always "charAt(6)", but the identifer at 6 changes:eg, charat(6) == 1, charat(6) ==2, etc; and it is always "display= inline".In the second call, both the charAt() number and the identifer at that number both change: eg, charAt(0) == a, charAt(0) == b, charAt(1) == a, charAt(1) == b. etc; and in this call, it's always "visibility= hidden".What I'm most uncertain about is how to treat the charAt() part of the "if" statements:

function dot(char6, DIS, charWhich, VIZ) {for (i=0;i<document.getElementsByTagName("span").length; i++) {var char6 = document.getElementsByTagName("span").item(i).id.charAt(6);		DIS = char6.item(i).style.display;var charWhich = document.getElementsByTagName("span").item(i).id.charAt();	// stumped here !	VIZ = charWhich.item(i).style.visibility }}

and the call would be something like

dot(char6,'inline','0' (or whatever number), 'hidden').

Grateful for anyone with the patience to follow this thru !

Link to comment
Share on other sites

[...] Grateful for anyone with the patience to follow this thru !
I haven't been following up to now, so I don't have the background for what you're trying to do, so forgive me if this comment is off the mark, but I have to say it seems overcomplicated to control program flow by looking up substrings of html element ids. If you would care to summarise what you actually need to achieve, perhaps someone can make another suggestion about the approach... as I say, sorry if this is off the mark. :) For what it's worth, you may use a variable as the argument to charAt. So if you have a way of knowing what this value is during your loop, and for instance it's in a variable called charIndex, you could then use charAt(charIndex).
Link to comment
Share on other sites

What is the char6 variable? Why do you pass it to the dot function if you just assign a local variable with the same name inside the function?
I agree it looks wrong: do you have a suggestion? Bear in mind you're dealing here with a cut'n'paster rather than a writer.
... you could then use charAt(charIndex).
I think this is what's needed.
it seems overcomplicated to control program flow by looking up substrings of html element ids
Multiple classes would have been one simpler way: I did try two getbyClass scripts, but each caused a conflict with other essential scripts based on getbyID, which I couldn't solve/get responses on... so I decided to do all manipulations (essentially there are three manipulations, this being the third) using IDs. It's clunky, but it works.As to what it's trying to do (apart from keeping Alzheimer's at bay :) ): if I said, "keeping track of the moving lines as you build a hexagram", that would be a short description. I could give you several links (google "I Ching"), but it's also useful for me to try to describe the process. Here goes:A "hexagram" is a diagram with six lines, one above another; each line is either solid ----- or broken -- -- . There are 64 combinations. You arrive at one of these fortune-cookies by making a random choice, six times, one for each line. Your random choice has only 4 outcomes: 6,7,8,9.6 and 8 reduce the field to a specific set—hexagrams with the first line broken; 7 and 9 to the complementary set. After six iterations, a single hexagram remains.I've set up the 64 hexagrams as a field, with each iteration reducing the possibilities, accomplished by manipulating a series of arrays. The arrays allow progressively turning off the unwanted hexagrams, rather than just switching them all off at once, which one could do using classes... We haven't gotten to the script in question yet.As well: At any iteration, an outcome of 6 or 9 changes the potential of that line, so that it will subsequently become its opposite, thus generating a second hexagram. In the parlance, 6 and 9 are "moving lines". Now, switching on and switching off the symbol associated with the moving lines requires as much specificity as the arrays associated with the hexagrams; but assigning 64 x 6 ids and then divvying them up into arrays looked a bit over the top. Instead I've coded the structure of the hexagram into the id of each of its moving-line-symbols, as well as having that id identify which line it relates to... hence, "charAt" as a way to turn the symbol on and off. The test page doesn't incorporate the randomising function: it just allows each outcome of each iteration to be tested, by clicking on the appropriate link. Each iteration will appear to grey out half the hexagrams (actually there's a layer of grey hexagrams beneath); then mark the line by turning it red; then mark if it is a moving line by placing a red dot (the script in question) to the right of the line.The final link, "second hexagram", shows the hexagram made when the moving lines change into their opposites. It's on a separate layer hitherto unseen. It's very slow tonight.
Link to comment
Share on other sites

[...] As to what it's trying to do (apart from keeping Alzheimer's at bay :) ): if I said, "keeping track of the moving lines as you build a hexagram", that would be a short description. I could give you several links (google "I Ching"), but it's also useful for me to try to describe the process. Here goes: [...]
Enjoyed your description.I should think your generic function will need three parameters:- c1Value: the value of the character to test for in if statement 1- c2Index: the charAt index for if statement 2- c2Value: the character value to test for in if statement 2and will look something like this:
function dot(c1Value, c2Index, c2Value) {for (i=0;i<document.getElementsByTagName("span").length; i++) {if (document.getElementsByTagName("span").item(i).id.charAt(6) == c1Value) { document.getElementsByTagName("span").item(i).style.display = "inline" }if (document.getElementsByTagName("span").item(i).id.charAt(c2Index) == c2Value) {document.getElementsByTagName("span").item(i).style.visibility = "hidden"}}}

and you'll obviously need to include the three relevant parameters in each setTimeout call:

setTimeout('dot(1, 0, \'a\')',4200);

Link to comment
Share on other sites

Many thanks! a a big credit... I won't get time to work on it for a couple of days. I was in two minds about launching into an explanation.best wishesctozi have the randomising function and a series of splits scripted: they'll be the next test.

Link to comment
Share on other sites

Generics in JavaScript? May be. I do not know the scripts (possibilities) well enough. Do you need generics to solve your problem?Generics generally.Generics, parametricing code, function objects and iterators on function objects are very important elements in C++,"the next assembler generation" (and C# I don't know it so well, but have seen parametriced code examples). You find some C++ examples on this subject on my forum:http://www.forumnorway.com/viewforum.php?f...0114994c9f13a78A very good C++ article:Andrew Koenig "Templates and generic algorithms" Journal of object oriented programming june 1994.May be that code, ( heading "A matrix class that illustrates the use of templates etc.") can inspire you. More than 10 years since I wrote that code.Note: Because of so much spam that forum is locked (only moderators can post and read all posts. Spammers and crackers shall not get everything for free. Unfortunately that has concequences for serious students.)Generics in PHP, may be possible in PHP 6.0 or lower for a clever programmer: http://www.php.net/~derick/meeting-notes.htmlSee also: http://www.sitepoint.com/forums/showthread.php?t=421181There may be possibilities with pholymorphic code like this:

<?phpclass Message {	var $message;	function Message() {}	function setMessage($message)	{		$this->message = $message;	}	function getMessage()	{		return $this->message;	}}class PoliteMessage extends Message {	function PoliteMessage()	{		$this->setMessage('How are you today?');	}}class TerseMessage extends Message {	function TerseMessage()	{		$this->setMessage('Howzit?');	}}class RudeMessage extends Message {	function RudeMessage()	{		$this->setMessage('You look like *%&* today!');	}}class MessageReader {	var $messages;	function MessageReader(& $messages)	{		$this->messages = & $messages;		$this->readMessages();	}	function readMessages()	{		foreach ( $this->messages as $message ) {			echo ( $message->getMessage().'<br />' );		}	}}$classNames = array ('PoliteMessage','TerseMessage','RudeMessage');srand ((float)microtime()*1000000);shuffle ($classNames);$messages = array();for ( $i = 0; $i < 10; $i++ ) {	shuffle($classNames);	$messages[] = new $classNames[0]();}$messageReader = new MessageReader($messages);?>

Source: Harry Fuecks (latest edition) "The PHP Anthology: Object Oriented PHP Solutions" Volume I

Link to comment
Share on other sites

I don't think he meant generic is terms of OOP, I think he just wanted to make the function applicable for a wider range of elements.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...