For loop skips values

243
January 12, 2018, at 05:03 AM

I have this program to recognize where an URL is within a message and leave in the string ONLY the URL, but it just skips values randomly and I din't know why. Here is my code and the debug console.

function DeleteNonURL(sep, URL)
{
    var _Pos = new Number;
    URL = URL.split(sep);
    for (var i = 0; i < URL.length; i++)//deletes non-url substrings
    {
    Debug.Trace("Msg: "+URL + " i: "+i + " _Pos: "+_Pos + " Current Substring: "+URL[i]);
        _Pos = URL[i].indexOf('.com');
        if(_Pos == -1)
        {
            _Pos = URL[i].indexOf('.net');
            if(_Pos == -1)
            {
                _Pos = URL[i].indexOf('.org');
                if(_Pos == -1)
                {
                    _Pos = URL[i].indexOf('.info');
                    if(_Pos == -1) //No URL in this substr
                    {
                        URL.splice(i, 1); //deleted.
                    }
                }
            }
        }
    }
    Debug.Trace(URL);
}
Debug:
Msg: Hey,dude,look,at,this,amazon.com,what,is,it? i: 0 _Pos: 0 Current Substring: Hey
Msg: dude,look,at,this,amazon.com,what,is,it? i: 1 _Pos: -1 Current Substring: look
Msg: dude,at,this,amazon.com,what,is,it? i: 2 _Pos: -1 Current Substring: this
Msg: dude,at,amazon.com,what,is,it? i: 3 _Pos: -1 Current Substring: what
Msg: dude,at,amazon.com,is,it? i: 4 _Pos: -1 Current Substring: it?
dude,at,amazon.com,is
Answer 1

As noted in the comments, this is the very common problem of mutating the array that you're iterating when the mutation causes the array to be reindexed.

Ultimately, you seem to be wanting to check for substring that ends with one of the TLDs. This can be done much more simply.

const re = /\.(?:com|net|org|info)$/; 
 
function test(sep, URL) { 
    return URL.split(sep).find(s => re.test(s)); 
} 
 
const a = "http://example.com/foo/bar"; 
const b = "foo/bar/baz"; 
 
console.log("TLD part", test('/', a)); 
console.log("TLD part", test('/', b));

Rent Charter Buses Company
READ ALSO
Jasmine spyOn without hardcoded string

Jasmine spyOn without hardcoded string

Is there an alternative to hardcoding the method name as parameter to spyOn?

306
React dynamic layout based on JSON file

React dynamic layout based on JSON file

While designing my ReactJS app I have a question with dynamic layout implementation (of my reading/news app) based on JSON fileFor now it's got to be only Main page (including listing of articles) and detail page for articles

266
Build tree structure in html using json

Build tree structure in html using json

I'm trying to build a dynamic tree structure in htmlI'm getting result from database in nested json format

346
React Native TextInput different background colors for different letters/characters?

React Native TextInput different background colors for different letters/characters?

I know I can set the background for a TextInput, but I want to change the background color of parts of the TextInput to highlight certain letters

338