开发者社区 问答 正文

只允许使用 + - _ / 和 Math._ ,求一个函数 y = f(x, a, b);当 x >

只允许使用 + - _ / 和 Math._ ,求一个函数 y = f(x, a, b);当 x > 100 时返回 a 的值,否则返回 b 的值,不能使用 if else 等条件语句,也不能使用|,?:,数组。

展开
收起
kun坤 2019-11-28 14:50:39 538 分享 版权
1 条回答
写回答
取消 提交回答
  • function f(x, a, b) {
      var temp = Math.ceil(Math.min(Math.max(x - 100, 0), 1));
      return a * temp + b * (1 - temp);
    }
    console.log(f(-10, 1, 2));
    
    
    2019-11-28 14:50:46
    赞同 展开评论
问答地址: