2

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;
}
4
  • Why does tileid = 0 correspond to the 7th link? Commented Feb 22, 2013 at 0:44
  • 3
    just use arithmetics and - 7 Commented Feb 22, 2013 at 0:45
  • You can either use a nested for loop inside the first for loop or you can put all the hrefs into an object and use direct object property lookup. Commented Feb 22, 2013 at 0:46
  • tileid = hrefs.indexOf(window.location) - 7 Commented Feb 22, 2013 at 0:56

3 Answers 3

4

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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;
  }
}

Comments

-1

You can do a switch statement.

switch(urlval){
    case 1:
     tileid = 1;
    break;

    case 2:
     tileid = 2;
    break;

    default:
     tileid = 0;
}

7 Comments

Using a switch statement still doesn't remove duplicate code, which is usually a sign that you're doing something wrong
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?
"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.
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).
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!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.