Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    m32
    m32 is offline
    New to the CF scene
    Join Date
    Jun 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    New Coder
    Join Date
    Mar 2005
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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."

  • #3
    New Coder
    Join Date
    Mar 2005
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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."

  • #4
    New Coder
    Join Date
    Mar 2005
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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."

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Posts
    400
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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)" />

  • #6
    m32
    m32 is offline
    New to the CF scene
    Join Date
    Jun 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •