Thursday, February 01, 2007

Dis Array

Trying to learn Javascript is frustrating and I'm tearing my hair out. I write a script for this blog that displays a random quotation at the head of the content section. If you click on the quote, it automagically switches to another quote. All good. I tighten up the code and I'm pleased with the effect. It's not expert level but it does work.

So I try to take the next step. I have the quote and author coded as two separate variables. But obviously they could be combined as two properties of one variable. This would be useful if I wanted to add a new quote, because I could add both together in one go.

But I can't make it work. I use what I think should be the right method, but it doesn't call up a quote.

(Just in case anyone knows Javascript out there, what I am doing is making an array called roche. I add entries to the array in the form {q: "blah blah blah", a: "bloke who said it"};. I randomise an index number (in a process I know works) and then ask for the write method of the document to give me roche[index].q and roche[index].a. Nada. I've tried it using a fixed number rather than a random one and that doesn't work either, so something is wrong with my array, but I can't work out what.)

Then I find it. I missed a comma in the last array entry. I am not kidding. The whole thing is fucked because the last entry in the array is fucked. It simply doesn't create the entire array!

Anyway, now it works. Of course, you probably didn't ever notice that the quote changes, but it does, every time you access the blog. I am loving my script (it's not perfect: I don't declare the variables properly and it could mostly be placed outside the body, but Blogger already compromises the structure/presentation divide sufficiently that I don't think a bit more will hurt). Go on; see for yourself. Click on the quote and watch it change. Better still, steal my script. Change my quotes for yours (it's easy, just put quote in "q" for each entry and author in "a"). Just don't use Rowling as your author, k?

3 Comments:

At 2:33 am, Anonymous Anonymous said...

Of course, you probably didn't ever notice that the quote changes...

Well, I knew that it changes, but since I can't read French worth shit, I can at least find one I understand :-)

Nice work.

 
At 5:08 am, Anonymous Anonymous said...

Not only had I noticed, but I also checked out your script some time ago. Not that it matters for the functioning of your script, but you seem to have misapplied the idea of a "seed".

 
At 7:27 am, Blogger Dr Zen said...

It's not a random seed if that's what you mean (as for instance using the Date object to seed it with a number) but it's a seed in the sense of this type of script (a number that you want to be randomised). The process is the same but the function is different because I'm not trying to create a random number.

 

Post a Comment

<< Home