...

View Full Version : Strings, arrays, and HTML DOM



m32
06-30-2005, 10:12 PM
I'm writing a page template for a webcomic. The idea is that a dirrectory will contain a bunch of files named, for example, comic001.jpg, comic002.jpg, comic003.jpg, and so on. What I'm trying to do is to have a link that the viewer can click to load the next page (if one is available).

I need to be able to extract the ### part of the file name and increment it by one. To that end I'm using a function to pull out the numbers from the URL as a string, and then another function to convert the string to an int type array and increment them by 1. It's not working though. :( Can someone tell me why?


function forward() {
var url = document.pic.src;
var periodlocation=url.lastIndexOf(".");
var filetype=url.substring(periodlocation);
var number=url.substring((periodlocation - 4), periodlocation);
var urlbase=url.substring(0, (periodlocation - 4));
var number = increment(number);
document.pic.src = (urlbase + number + filetype);
}

function increment(str) {
var numbers = Array(str.length);
var numberlist = "0123456789";
for (var x = 0; x < str.length; x++) {
numbers[x] = numberlist.indexOf(str.charAt(x));
}
var i = 1;
while (i <= numbers.length) {
if (numbers[numbers.length - i] < 9) {
numbers[numbers.length - i] = (numbers[numbers.length - i] + 1);
break;
}
esle {
numbers[numbers.length - i] = 0;
i++;
}
}
var result = "";
for (var a = 0; a < numbers.length; a++) {
result = result + numbers[a];
}
return result;
}

Also, what little background I have is in Java and this is only the second JavaScript that I have ever writen, so be kind to the newb, k?

WMJB
07-01-2005, 12:47 AM
there were two errors...

one... you had esle instead of else.. and two, you needed to use -3 instead of -4


function forward() {
var url = document.pic.src;
var periodlocation=url.lastIndexOf(".");
var filetype=url.substring(periodlocation);
var number=url.substring((periodlocation - 3), periodlocation);
alert(number);
var urlbase=url.substring(0, (periodlocation - 3));
var number = increment(number);
document.pic.src = (urlbase + number + filetype);
}

function increment(str) {
var numbers = Array(str.length);
var numberlist = "0123456789";
for (var x = 0; x < str.length; x++) {
numbers[x] = numberlist.indexOf(str.charAt(x));
}
var i = 1;
while (i <= numbers.length) {
if (numbers[numbers.length - i] < 9) {
numbers[numbers.length - i] = (numbers[numbers.length - i] + 1);
break;
}
else {
numbers[numbers.length - i] = 0;
i++;
}
}
var result = "";
for (var a = 0; a < numbers.length; a++) {
result = result + numbers[a];
}
return result;
}

WMJB
07-01-2005, 12:48 AM
I left an alert() in the script so you could see how to check for these errors in the future...

good luck.

WMJB
07-01-2005, 01:07 AM
And here is a much simpler version of the increment function:


function increment(str) {
number = str;
number++;
if(number<10){ result = "00" + number;}
else if(number<100){ result = "0" + number;}
else {result = number;}
return result;
}

Harry Armadillo
07-01-2005, 01:43 AM
And here's a smaller, more powerful version (based on lamdba functions :D).
var first=0,last=185;

Number.prototype.toFixedInt=function(n){
var a=Math.round(this).toString();
while(a.length<n)
a="0"+a;
return a
}
function comicStrip(func){
var url = document.pic.src;
document.pic.src = url.replace(/(\d+)/,func);
}
function increment(match){
var n=Number(match)+1
if(n>last)
n=last;
return n.toFixedInt(match.length);
}
function decrement(match){
var n=Number(match)-1;
if(n<first)
n=first;
return n.toFixedInt(match.length);
}
There's a couple of assumptions at work: first, that the only numbers present in the full URL of the comics is the, uh, comic's number. Yeah. The number used to number the comics. :p Second, that you'll supply numbers for first and last (the number won't increment/decrement beyond first and last).

You'd call mine like:

comicStrip(increment);

or

comicStrip(decrement);

or more likely

<img src="images/next.gif" title="Next Comic" onclick="comicStrip(increment)" />

m32
07-01-2005, 01:46 AM
Ah, thank you much. I had a feeling it was going to be something simple. I had been over that code for almost two hours and I couldn't find the problem. Thanks again.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum