dimanche 1 septembre 2019

Ways to troubleshoot if/else statement in JS?

In a function, I have an if/else statement which runs through conditions, then each "else" returns a single variable. When run, the function should return the appropriate variable. Currently, it defaults to the final "else if", right before my "else" statement. I haven't had this happen before so any guidance on where to start troubleshooting will be appreciated.

The function is in the <head> of an HTML site and is designed to redirect the user to a new site, with the new site being dictated by the if/else statement. I'm using Virtual Studio Code and tried [View -> Problems], but it indicates there are no issues. Below is a tidbit which shows what I'm dealing with.

<head>
var Trgt1 = location.replace("site1.com");
var Trgt2 = location.replace("site2.com");

function go2() {
    if (condition) { // the actual conditions are lengthy, but set to variables in the header and called in each condition's parenthesis.
      return Trgt1;
    } else if (other condition) {
      return Trgt2;
    }
}
</head>
<body onload="go2()">
  [body content]
</body>

I'd like my if/else statement to return one of any of its options, but currently it's defaulting to the final "else if" in the sequence. I do have an "else" at the end, but it seems to be ignored with the rest of them.

Aucun commentaire:

Enregistrer un commentaire