1

whats the problem with backslash and parentheses in this code? it spesificly appears in "else if" line

function roofFix(s, x){
  if(x.includes("/")){
     var xIndex1 = x.indexOf("/");
  
    if( s[xIndex1] == " " ){
        return true
      }else{
        return false
      }
  }else if(x.includes("\")){
    if( s[xIndex1] == " " ){
        return true
      }else{
        return false
      }
  
  }else{
    return false
  }
 


}
roofFix('   h c ', '__/____')
Andreas
  • 21,535
  • 7
  • 47
  • 56
emyy96
  • 199
  • 1
  • 8

2 Answers2

1

\ is used for escape character. If you want to compare with \ use \\.

function roofFix(s, x){
  if(x.includes("/")){
     var xIndex1 = x.indexOf("/");

    if( s[xIndex1] == " " ){
        return true
      }else{
        return false
      }
  }else if(x.includes("\\")){
    if( s[xIndex1] == " " ){
        return true
      }else{
        return false
      }

  }else{
    return false
  }



}
roofFix('   h c ', '__/____')
1

With the \ you can escape special characters in a string. Right now you escape the " character. So add another \ to escape the backslash character, like below. And it should work.

function roofFix(s, x){
  if(x.includes("/")){
     var xIndex1 = x.indexOf("/");
  
    if( s[xIndex1] == " " ){
        return true
      }else{
        return false
      }
  }else if(x.includes("\\")){
    if( s[xIndex1] == " " ){
        return true
      }else{
        return false
      }
  
  }else{
    return false
  }
 


}
roofFix('   h c ', '__/____')