遇到个沙雕面试题......
//函数效果输入任何数都可以计算 参数的详细类型判断,可以自己加
function add(start, end) {
// 参数类型限定,未做大于0的限制 负数也能玩吧?end大于start.if判断也不会触发
if (Number.isInteger(start) || Number.isInteger(end)) return throw new Error("输入整数");
let count = 0
count += start
if (start + 1 <= end) {
count += add(start + 1, end)
}
console.log(start, "count:", count); //在上面的循环回调完成之后,返回的count就是我们需要的
return count
}
add(1, 100) //1-100的结果是5050都深入人心了吧?自测