js中的两个数字a,b求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。一共b个数字相加,例如用户输入2,5 s=2+22+222+2222+22222
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript">
var num = "";
var nums = [];
var a = 2;//所要拼接的数字
var b = 5;//所要拼接的位数
for(var i = 0; i < b; i ++){
num += a;
nums.push(parseInt(num));
}
var sum = 0;
for(var i = 0; i < b; i ++){
sum += nums[i];
}
document.write(sum + "=" + nums.join("+"));
</script>
</head>
<body>
</body>
</html>