累加累乘
作者:互联网
累加累乘
累加:
// 求和: s = 1+2++3+...+n
var s = 0;
var res = 0;
for (var i = 1; i < n+1; i++) {
// 拼接字符串
s += '+' + i
// 累加
res += i
if (i === n-1) {
console.log(s + "=" + res)
} else {
console.log(s)
}
}
累乘:
// 累乘: s = 1*2*3*4*5...*n
var s = 1;
var r = ' '
for(var i = 1;i < n+1;i++){
r += i +"x"
s *= i
t = r.slice(0,r.length-1)
var res = t +'='+s
console.log(res)
}
标签:console,log,res,累加,累乘,var 来源: https://blog.csdn.net/ly_521_xsy/article/details/99989757