在 if else 语句中发出

issue in if else statement

我需要帮助来解决我遇到的这个问题。我已经解决了 Whosebug 中可用的几个问题,但我仍然不明白。

这是我正在写的代码:

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)

这是我得到的错误:

每个if部分和最后else部分都包含return,所以控件不能通过if-elseif-elseif-else结构。因此最后 console.Log 行无法访问。

删除所有内部 return 语句并在末尾添加一个以获得您想要的:

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;

如果你想访问控制台日志,你应该像这样重组,所以最后一行是可以访问的。

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;

您必须为 myFeedback 变量的决定传递一个 return,并且 return 应该放在末尾。 请参阅下面的示例大纲

      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;