Sean W
Sean W

Reputation: 407

Else if statement executes despite first condition is met

I have a patch of code that is intended to perform a search across a sheet then assign a variable if found.

Here's the code:

  for(i=0;i<values.length; i++)
      {
        rowvalue = values[i];        
        for (j=l=1;j<rowvalue.length; j++) {
             colvalue = rowvalue[j];             
             colvalue1 = colvalue.toString();             
             if(colvalue1.indexOf("search1")>-1 && colvalue1.indexOf("search2")==-1
                && rowvalue[j+5] == "") {
                rowp = i+1, colp = j+1;               
                var itemcode = 9;
      }
    else if(colvalue1.indexOf("search1")>-1 && colvalue1.indexOf("search2")==-1) {
                rowp = i+1, colp = j+1;               
                var itemcode = 9;
                }

When executed, it will always return the value that met the 2nd condition(else if), despite the first condition was met. If I remove the else if statements, then I will get the correct return value.

As far as I know, once if statement was fulfilled, it's supposed to skip else or else if. But for some reason this wasn't the case in my situation. I'm not sure if I'm using the function properly or I'm misunderstanding on how the function work.

Upvotes: 0

Views: 118

Answers (3)

pointNclick
pointNclick

Reputation: 1632

Try doing the same function this way:

if(colvalue1.indexOf("search1")>-1 && colvalue1.indexOf("search2")==-1 ) 
{
  if (rowvalue[j+5] == "")
  {
    rowp = i+1, colp = j+1;
    var itemcode = 9;
  }else{
    rowp = i+1, colp = j+1;               
    var itemcode = 9;
  }
} 

Upvotes: 1

zelalem fikre
zelalem fikre

Reputation: 1

single closing curly brace before the else statement should resolve the issue.

Upvotes: 0

Dijkgraaf
Dijkgraaf

Reputation: 11527

If you indent your code correctly it becomes rather obvious where it goes wrong

Your code indented.

    for(i=0;i<values.length; i++)
    {
        rowvalue = values[i];        
        for (j=l=1;j<rowvalue.length; j++) 
        {
            colvalue = rowvalue[j];             
            colvalue1 = colvalue.toString();             
            if(colvalue1.indexOf("search1")>-1 && colvalue1.indexOf("search2")==-1 && rowvalue[j+5] == "") 
            {
                 rowp = i+1, colp = j+1;
                 var itemcode = 9;
                 else if(colvalue1.indexOf("search1")>-1 && colvalue1.indexOf("search2")==-1) 
                 {
                     rowp = i+1, colp = j+1;               
                     var itemcode = 9;

                 }

What you probably meant was to as below, please note the "Added missing closing curly bracket"

for(i=0;i<values.length; i++)
{
    rowvalue = values[i];        
    for (j=l=1;j<rowvalue.length; j++) 
    {
        colvalue = rowvalue[j];             
        colvalue1 = colvalue.toString();             
        if(colvalue1.indexOf("search1")>-1 && colvalue1.indexOf("search2")==-1 && rowvalue[j+5] == "") 
        {
             rowp = i+1, colp = j+1;
             var itemcode = 9;
        } // Added missing closing curly bracket
        else if(colvalue1.indexOf("search1")>-1 && colvalue1.indexOf("search2")==-1) 
        {
            rowp = i+1, colp = j+1;               
            var itemcode = 9;

        }
     }
}

Upvotes: 1

Related Questions