0

这是挑战:

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

创建一个接受布尔值和字符串的函数 callFriend。如果布尔值为真,则 callFriend 应返回字符串“本周末使用 NAME 制定的计划”。否则它应该返回“这个周末每个人都很忙”。>

这是我写的:

let friendsAvailable = true;

function makePlans(name) {
  return callFriend(friendsAvailable, name);
}

function callFriend(bool, name) {
  if (bool = true) {
    return 'Plans made with ' + (name) + ' this weekend'
  } else {
    'Everyone is busy this weekend'
  }

}

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."

4

2 回答 2

2

除了if (bool = true)大家已经指出的部分(你可以用if (bool)这个),你忘了添加return声明else。它应该是:

} else {
    return 'Everyone is busy this weekend'
}
于 2022-01-06T18:42:52.583 回答
-3

完整代码:

let friendsAvailable = true;

function makePlans(name)
  {
  return callFriend(friendsAvailable, name);
  }

function callFriend(bool, name)
  {
  if (bool)  // or  if (bool===true), but testing if true is true is a little bit redundant
    {
    return 'Plans made with ' + (name) + ' this weekend'
    } 
  else 
    {
    return 'Everyone is busy this weekend'
    }
  }

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."

但是,如果您想给老师留下深刻印象,请执行以下操作:

const
  callFriend = 
    (bool, name) =>
      bool 
       ? `Plans made with ${name} this weekend` 
       : 'Everyone is busy this weekend' 

const makePlans = name => callFriend(friendsAvailable, name);


let friendsAvailable = true

console.log(makePlans('Mary')) 

friendsAvailable = false

console.log(makePlans('James')) 

一些助手:
箭头函数表达式
条件(三元)运算符

于 2022-01-06T18:46:48.600 回答