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 5 of 5

Thread: Array problem

  1. #1
    Regular Coder WAINGRO's Avatar
    Join Date
    Feb 2016
    Posts
    194
    Thanks
    30
    Thanked 5 Times in 5 Posts

    Array problem

    Hi all I am trying to create a function that determines whether or not a given string (parameter 1) ends with another given string (parameter 2)
    strangely enough, in one of the lines in the end of the code, the .join method doesnt work... Does anybody know why? It logs an array, and not a string...I am talking about this line of code: var stringEnd = compareBox1.join();
    console.log(stringEnd);


    Code:
    
    function confirmEnding(str, target) {
      // "Never give up and good luck will find you."
      // -- Falcor
      
      
      //MAAK TWEE ARRAYS
      //EACH ARRAY CONTAINS ARRAY.LENGTH LETTERS
      //THE INDEXNUMBER OF LAST ITEM IS ARRAY.LENGTH-1
      var sBox = str.split("");    
      console.log(sBox);
      var sLength = sBox.length;
        console.log(sLength);
      var tBox = target.split("");
      console.log(tBox); 
      var subNumber = tBox.length;
      console.log(subNumber);
      var startingPoint = sLength - subNumber;
      console.log(startingPoint);
      
      //loop from (last item - targetlength - 1) till end to make a string that will be compared later
        compareBox1 = [];
      
    
    
      
      for (var i= startingPoint; i<sLength; i++ ) {
      	      console.log("i has the number " +i);
        compareBox1.push(sBox[i]);
    
    
        if (i === sLength - 1 ) {
    	    
        var stringEnd = compareBox1.join();
        console.log(stringEnd);
        console.log(target);
           if ( stringEnd === target ) {
            
            console.log( true);
        }   
        else { 
            console.log( false);
        }
       
        }
    
    }
    
    }
    
    confirmEnding("He has to give me a new name", "name")
    Certified DeathShadow fan @ www.cutcodedown.com

  2. #2
    Senior Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    1,014
    Thanks
    182
    Thanked 126 Times in 126 Posts
    Hi

    try

    var stringEnd = compareBox1.join('');


    from w3schools:
    The join() method joins the elements of an array into a string, and returns the string.

    The elements will be separated by a specified separator. The default separator is comma (,).

    LT
    0000

  3. #3
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    1,465
    Thanks
    2
    Thanked 219 Times in 209 Posts
    Uhm, as of IE9 this functionality already EXISTS in all browsers... and even if it didn't I'm not sure why you're screwing around with join or arrays or any of that and not just using substr and a bit of math.

    https://developer.mozilla.org/en-US/...tring/endsWith

    Code:
    var test = 'This is a test';
    alert(test.endsWith('test');
    You want compatibility in older browsers, the polyfill on MDN is adequate... though I'd be doing it something more like:

    Code:
    if (!String.prototype.endsWith) {
    	String.prototype.endsWith = function(needle, position) {
    		if (needle.length == 0) return true; // empty is always present
    		var
    			haystack = this.toString(), // safety for call() or forced typecast
    			searchStart = (
    				typeof position == 'number' ?
    				Math.min(position, haystack.length) :
    				haystack.length
    			) - needle.length - 1;
    		return
    			searchStart < 0 ?
    			false :
    			needle == haystack.substr(searchStart, needle.length);
    	};
    }
    Actually extracting the value.

    Honestly I can't even figure out how the method you are trying to use with join(), split() and so forth is even supposed to work.
    I would rather have questions that can't be answered, than answers that can't be questioned.
    http://www.cutcodedown.com

  4. #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    19,246
    Thanks
    212
    Thanked 2,672 Times in 2,648 Posts
    Quote Originally Posted by deathshadow View Post
    Uhm, as of IE9 this functionality already EXISTS in all browsers
    No it doesn't, it is not curently supported by IE and Opera.

    Code:
    var str = "Never give up and good luck will find you.";
    var pattern = /you.$/;  // test for substring you. at end of string
    var res = pattern.test(str);
    alert (res);
    Last edited by Philip M; 01-13-2017 at 02:02 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  5. Users who have thanked Philip M for this post:

    deathshadow (01-14-2017)

  6. #5
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    1,465
    Thanks
    2
    Thanked 219 Times in 209 Posts
    Quote Originally Posted by Philip M View Post
    No it doesn't, it is not curently supported by IE and Opera.
    My bad, *** egg on face ***, you're right; it was added with Edge.

    ... and yeah, a regex is another good answer to the problem.
    I would rather have questions that can't be answered, than answers that can't be questioned.
    http://www.cutcodedown.com


 

Tags for this Thread

Posting Permissions

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