I'm looking for an easier way to process through these multiple If Else statements, there are several hundred. I'm guessing it would be best to just use another For loop inside of the If Else, but let me know you all suggest, thanks!
var hrefs = [];
var list = document.getElementsByTagName("a");
for (var i=0; i<list.length; i++) {
var hey = list[i].href;
hrefs.push(hey);
}
if(window.location == 'http://') {
tileid = <?php echo $numposts.''; ?>;
} else if (window.location == hrefs[7]) {
tileid = 0;
} else if (window.location == hrefs[8]) {
tileid = 1;
} else if (window.location == hrefs[9]) {
tileid = 2;
} else if (window.location == hrefs[10]) {
tileid = 3;
} else if (window.location == hrefs[11]) {
tileid = 4;
} else if (window.location == hrefs[12]) {
tileid = 5;
} else if (window.location == hrefs[13]) {
tileid = 6;
} else if (window.location == hrefs[14]) {
tileid = 7;
} else if (window.location == hrefs[15]) {
tileid = 8;
} else if (window.location == hrefs[16]) {
tileid = 9;
} else if (window.location == hrefs[17]) {
tileid = 10;
} else if (window.location == hrefs[18]) {
tileid = 11;
} else {
tileid = window.history.state.projid;
}
asked Feb 22, 2013 at 0:43
Jordan
1,4573 gold badges19 silver badges25 bronze badges
3 Answers 3
Please try this:
var flag = false;
if (window.location == 'http://') {
tileid = <?php echo $numposts.''; ?>;
} else {
for (var i = 7; i <= 18; ++i) {
if (window.location == hrefs[i]) {
tileid = i - 7;
flag = true; break;
}
}
if (!flag) tileid = window.history.state.projid;
}
answered Feb 22, 2013 at 0:46
David G
97.6k41 gold badges173 silver badges258 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Not tested this, and i think i<(list.length<18?list.length:18) might be wrong. Its just to stop processing links, assuming you dont need anymore than 18.
var hrefs = {};
var list = document.getElementsByTagName("a");
for (var i=0; i<(list.length<18?list.length:18); i++) {
hrefs[list[i]] = i;
}
if (window.location == 'http://'( {
tileid = <?php echo $numposts.''; ?>;
} else {
if (!hrefs[window.location]) {
tileid = window.history.state.projid;
} else {
tileid = hrefs[window.location] - 7;
}
}
answered Feb 22, 2013 at 0:58
NickSlash
5,1954 gold badges25 silver badges41 bronze badges
Comments
You can do a switch statement.
switch(urlval){
case 1:
tileid = 1;
break;
case 2:
tileid = 2;
break;
default:
tileid = 0;
}
7 Comments
miguel.martin
Using a switch statement still doesn't remove duplicate code, which is usually a sign that you're doing something wrong
Just Aguy
If its an instance where a switch is applicable, it's not duplicate code. Do you actually develop? I posted an alternative to an already answered question. He asked for a different way than using ifs. If that doesn't fit his exact scenario then so be it but its a very viable alternative. Don't vote me down because you don't understand. Do you know what this is for?
miguel.martin
"Do I actually develop?" Did you just seriously ask me that? All I said was: you ARE duplicating code. The way you implemented the switch could be done in ONE LINE.
tileid = urlval;, or tileid = (urlval != SOME_VALUE_I_DONT_WANT ? urlval : DEFAULT_VALUE);. Don't be so rude, I'm just clearly saying that you are duplicating code. He did ask for an alternative, but he asked for an easier way than using if/else-if's. Using a switch doesn't make this any easier; just a little easier to read, but it's stil messy and not very maintainable.miguel.martin
Also, I'd like to state that OP said he has to process through a several hundred, does he really want to copy/paste over 100 times? Using a switch doesn't stop the copying and pasting (i.e. duplicate code). And what if it turns out he needs to process more, that means more copy and pasting. Look at David's answer, it's what I would of done, except with less constants, so it's easier to change later (if I needed to).
Just Aguy
All of this is about being open-minded, objective, sharing ideas and methods of doing things. There is absolutely nothing to be gained from your first, second, or third comment. The first was nothing more than a snide remark followed by worthless dribble. Go away, you're bothering me!
|
lang-js
tileid = 0correspond to the 7th link?- 7forloop inside the firstforloop or you can put all the hrefs into an object and use direct object property lookup.tileid = hrefs.indexOf(window.location) - 7