typescriptif-statementunreachable-code

issue in if else statement


I need help to solve this issue that I am having. I have went through few question available in stackoverflow but i still dont get it.

this is the code im writing:

var myFeedback = ""
      if (this.userFriendlinessRating <= 4.0 && this.userFriendlinessRating >= 3.5){
        myFeedback = 'VERY SATISFIED' 
        return myFeedback;
      } else if (this.userFriendlinessRating <= 3.5 && this.userFriendlinessRating >= 2.5){
        myFeedback = 'SATISFIED' 
        return myFeedback;
      } else if (this.userFriendlinessRating <=2.5 && this.userFriendlinessRating >=1.5){
        myFeedback = 'NOT SATISFIED' 
        return myFeedback;
      } else {
        myFeedback = 'VERY NOT SATISFIED' 
        return myFeedback;
      }
      console.log(myFeedback)

and this is the error that i get:

unreachable code


Solution

  • If you want to access the console log you should restructure like this, so the last line is reachable.

    var myFeedback = ""
    if (this.userFriendlinessRating <= 4.0 && this.userFriendlinessRating >= 3.5){
      myFeedback = 'VERY SATISFIED' 
    } else if (this.userFriendlinessRating <= 3.5 && this.userFriendlinessRating >= 2.5){
      myFeedback = 'SATISFIED' 
    } else if (this.userFriendlinessRating <=2.5 && this.userFriendlinessRating >=1.5){
      myFeedback = 'NOT SATISFIED' 
    } else {
      myFeedback = 'VERY NOT SATISFIED' 
    }
    console.log(myFeedback)
    return myFeedback;