【问题标题】:Creating a function that accepts a string & creating a function that accepts a boolean value and a string创建一个接受字符串的函数并创建一个接受布尔值和字符串的函数
【发布时间】:2021-02-16 14:05:51
【问题描述】:

创建一个接受字符串的函数ma​​kePlans。这个字符串应该是一个名字。 ma​​kePlans 函数应该调用 callFriend 函数并返回结果。 callFriend 接受一个布尔值和一个字符串。将 friendsAvailable 变量和名称传递给 callFriend。

创建一个接受布尔值和字符串的函数callFriend。如果布尔值为真,callFriend 应返回字符串'Plans made with NAME this week'。否则它应该返回'本周末大家都很忙'。

到目前为止,这是我想出的: 让friendsAvailable = true;

function makePlans(name) {
  // INSERT CODE HERE
  return `Plans made with ${name} this weekend`;
                
}

function callFriend(bool, name) {
  // INSERT CODE HERE  
  if (callFriend = true); {
    return 'Plans made with ${name} this weekend';
  } else (callFriend = false)  {
    return "Everyone is busy this weekend";
  }
}
// Uncomment these to check your work!
 console.log(makePlans("Mary")) // should return: "Plans made with Mary this weekend'
 friendsAvailable = false;
 console.log(makePlans("James")) //should return: "Everyone is busy this weekend."

我无法确定要更正的内容,控制台告诉我存在语法错误:意外标记“else”

【问题讨论】:

  • 我认为if (callFriend = true); 之后的分号是你的问题。此外,通常在您发布问题时用您使用的语言标记您的问题会很有帮助。

标签: string function if-statement boolean


【解决方案1】:

if (callFriend = true); 行中有一个额外的;

; 有效地终止了if 条件。它后面的大括号只是被视为封装块,这意味着当编译器到达 else 时,它会感到困惑,因为它认为前面的 if 语句已经完成。

【讨论】:

    【解决方案2】:
    So I managed to put 
    
    
        let friendsAvailable = true;
    
    function makePlans(name) {
      // INSERT CODE HERE
    
      return callFriend(friendsAvailable, name);
                
    }
    
    function callFriend(bool, name) {
      // INSERT CODE HERE  
      if (bool) {
        return (`Plans made with ${name} this weekend`);
      } else  {
        return "Everyone is busy this weekend";
      }
    }
    
    // Uncomment these to check your work!
    console.log(makePlans("Mary")) // should return: "Plans made with Mary this 
    weekend'
    friendsAvailable = false;
    console.log(makePlans("James")) //should return: "Everyone is busy this weekend."
    

    【讨论】:

      猜你喜欢
      • 2021-11-29
      • 2020-03-03
      • 1970-01-01
      • 2019-03-19
      • 1970-01-01
      • 1970-01-01
      • 2021-03-14
      • 2018-06-17
      • 1970-01-01
      相关资源
      最近更新 更多