Antonio Ortiz Antonio Ortiz - 5 months ago 13
Javascript Question

Can't seem to get the break statement working correctly in conditional and for-loop in JavaScript

I am looping through a collection of links in the DOM and trying to set an attribute based on a condition. While doing so i'd like to strip out a specific string after the base URL:

http://smodev.rye.avon.com/XXXX/page.aspx


so I'd get:

page.aspx


So far I managed to get the base URL off, and at first I thought to use a regex, to zero in on
/XXXX/
, but I'd like to use this script in another context, because at other instances having just the pathname is swell:

/XXXX/XXXX/page.aspx


Naturally I thought using a
conditional
with the
includes
and
replace
methods in the following would work.

var MarkUpChecker = (function iffe() {
var publicAPI = {
getURL: function() {
for (var i = 0; i < arguments.length; i++) {
return {
'smo': 'http://smo.rye.avon.com',
'smodev': 'http://smodev.rye.avon.com',
'youravon_sans_w': 'http://youravon.com',
'youravon': 'http://www.youravon.com',
'youravon2': 'http://www2.youravon.com'
}[arguments[i]];
}
},
searchURL: function() {
var link, url, parser, newPathName = '';
for (var i = 0, len = arguments.length; i < len; i++) {
url = this.getURL(arguments[i]);
for (var j = 0, jlen = document.links.length; j < jlen; j++) {
link = document.links[j];
if (link.href.indexOf(url) !== -1) {
parser = document.createElement('a');
parser.href = link.href;

link.setAttribute('target', '_self');
newPathName = parser.pathname;


if (newPathName.includes('/Executive/')) {
var newstr = newPathName.replace('/Executive/', '');
break;
}
if (newPathName.includes('/District/')) {
newstr = newPathName.replace('/District/', '');
break;
}
if (newPathName.includes('/Division/')) {
newstr = newPathName.replace('/Division/', '');
break;
}
if (newPathName.includes('/National/')) {
newstr = newPathName.replace('/National/', '');
break;
}
link.href = newstr;

}

}
}
}
};
return publicAPI;
})();


When stepping through the debugger I noticed in the first pass it works!
\0/
However as the loop progresses, It becomes a mess as you can see here:

enter image description here

Shouldn't the
newstr
variable just return:

/XXXX/XXXX/XXXX/page.aspx


Otherwise just return

page.aspx


when it encounters
'/Executive/', '/District/', '/Division/', '/National/'

Answer

break is not doing what you expect -- it is not like in a case statement -- it makes you exit the inner for loop!

Secondly, if none of the if conditions is true, you assign a newstr value that is either undefined, or the value set during a previous iteration. That really causes a tremendous mix-up.

Instead use else:

if (newPathName.includes('/Executive/')) {
    var newstr = newPathName.replace('/Executive/', '');
} else if (newPathName.includes('/District/')) {
    newstr = newPathName.replace('/District/', '');
} else if (newPathName.includes('/Division/')) {
    newstr = newPathName.replace('/Division/', '');
} else if (newPathName.includes('/National/')) {
    newstr = newPathName.replace('/National/', '');
}

But looking at what you try to do here, you can do that in one replace:

var newstr = newPathName.replace('/Executive|District|Division|National/', '');

If your goal is to remove the path completely when one of these words occurs, then do this:

if(newPathName.search(/Executive|District|Division|National/) != -1) {
    newPathName = newPathName.split('/').pop();
}
linkhref = newPathName;
Comments