-2

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

CiaPan
  • 9,381
  • 2
  • 21
  • 35
YOS
  • 15
  • 5

3 Answers3

2

Every if part and the last else all contain return, so the control can not pass past the if-elseif-elseif-else structure. Hence the last console.Log line is unreachable.

Drop all inner return statements and add one at the end to get what you intend:

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;
CiaPan
  • 9,381
  • 2
  • 21
  • 35
  • thank you it works. will it cause a problem if i put the return in the last else ? – YOS Apr 13 '21 at 07:34
  • I have no idea what would be a problem to you. It depends on the desired behavior of your program. You can put `return` almost anywhere in the routine, but you need to understand what you want to achieve and how the code change serves your goals. Random modifications usually do not produce good programs but rather a total mess. :) It's like dumping a bag of lego blocks on a floor in hope you'll get a castle with five towers and three gates as a result. – CiaPan Apr 13 '21 at 07:52
  • @YOS Forgot to ping in the comment reply above, so pinging here. – CiaPan Apr 13 '21 at 08:01
2

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;
michjnich
  • 2,796
  • 3
  • 15
  • 31
  • just around the time you guys were commenting. i deleted the return and put it in the last else and it works. i saw you guys put the return outside the if statement. will it cause an issue if i put the return in the last else ? – YOS Apr 13 '21 at 07:35
  • If you put it in the last else, it will return and not do the `console.log` in that instance only. So it depends on when you want to run the console log. – michjnich Apr 13 '21 at 07:56
0

You have to pass a single return for your decision of myFeedback variable and that return should be placed at the end. See example outline below

      var obj = ""

      if (cond){
        obj = 'VERY SATISFIED' 
      } else if (cond){
        obj = 'SATISFIED' 
      } else if (cond){
        obj = 'NOT SATISFIED' 
      } else {
        obj = 'VERY NOT SATISFIED' 
      }
      console.log(obj)
      return obj;
CiaPan
  • 9,381
  • 2
  • 21
  • 35
Abdullah Mujahid
  • 888
  • 1
  • 12
  • 34
  • Your snippet doesn't work. It does not define the symbol `cond` it uses, and even if it did, the logics is obviously wrong – the two inner `if` blocks are unreachable. Additionally, you may not `return` if you're not in a block, e.g. a function. Either give a complete, working snippet, or don't make an unusable 'runnable snippet' from a pseudocode or a code sketch. – CiaPan Apr 13 '21 at 08:06
  • @CiaPan fyi I have assumed that cond is some condition. Understand the logic it's important to understand the structure rather than jumping directly solving it. The code snippet I have attached is explaining the workflow how if conditions are handled – Abdullah Mujahid Apr 13 '21 at 10:02
  • It's perfectly okay to post a simplified code of even a pseudocode if you just mean to explain the algorithm structure. What's not okay (IMHO) is suggesting it's a fully functional, runnable code, as you did. Just click the [Run code snippet] button and see results. – CiaPan Apr 13 '21 at 10:07
  • Ok got your point @CiaPan. Sorry my mistake I shouldn't have used that snip, I am quiet new to Stack, your constructive argument really helped me, will keep that in mind next time. Thankyou – Abdullah Mujahid Apr 13 '21 at 15:45
  • I modified your entry a bit to avoid suggesting the code is complete. Please see [revisions list](https://stackoverflow.com/posts/67070569/revisions) for my changes. Feel free to rollback if you don't like what I did. – CiaPan Apr 13 '21 at 19:35
  • Thankyou no its fine. – Abdullah Mujahid Apr 14 '21 at 12:59