Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-30-2005, 10:12 PM   PM User | #1
m32
New to the CF scene

 
Join Date: Jun 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
m32 is an unknown quantity at this point
Strings, arrays, and HTML DOM

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?

Code:
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?

Last edited by m32; 06-30-2005 at 10:17 PM.. Reason: typo
m32 is offline   Reply With Quote
Old 07-01-2005, 12:47 AM   PM User | #2
WMJB
New Coder

 
Join Date: Mar 2005
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
WMJB will become famous soon enough
there were two errors...

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

Code:
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;
}
__________________
"It doesn't matter if I'm optomistic or not, nothing ever works out for me."
WMJB is offline   Reply With Quote
Old 07-01-2005, 12:48 AM   PM User | #3
WMJB
New Coder

 
Join Date: Mar 2005
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
WMJB will become famous soon enough
I left an alert() in the script so you could see how to check for these errors in the future...

good luck.
__________________
"It doesn't matter if I'm optomistic or not, nothing ever works out for me."
WMJB is offline   Reply With Quote
Old 07-01-2005, 01:07 AM   PM User | #4
WMJB
New Coder

 
Join Date: Mar 2005
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
WMJB will become famous soon enough
And here is a much simpler version of the increment function:

Code:
function increment(str) {
	number = str;
	number++;
	if(number<10){ result = "00" + number;}
	else if(number<100){ result = "0" + number;}
	else {result = number;}
	return result;
}
__________________
"It doesn't matter if I'm optomistic or not, nothing ever works out for me."
WMJB is offline   Reply With Quote
Old 07-01-2005, 01:43 AM   PM User | #5
Harry Armadillo
Regular Coder

 
Join Date: Feb 2005
Posts: 400
Thanks: 0
Thanked 0 Times in 0 Posts
Harry Armadillo is on a distinguished road
And here's a smaller, more powerful version (based on lamdba functions ).
Code:
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. 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)" />
Harry Armadillo is offline   Reply With Quote
Old 07-01-2005, 01:46 AM   PM User | #6
m32
New to the CF scene

 
Join Date: Jun 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
m32 is an unknown quantity at this point
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.
m32 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:13 AM.


Advertisement
Log in to turn off these ads.