某员工入职信息为10000,薪资每年涨幅为5%,计算该员工50年后的薪水为多少.
递归,循环都可以
建议还是自己写,就算写错了最起码你去做了,不然下次你遇到了还是不会
let total = Array.from(Array(50)).reduce((pre,i ) => pre += (pre* 0.05), 10000);
// total = 114673.99785753674
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<script>
// 1
var num = 50, salary = 10000;
while (num-- > 0) {
salary += parseInt(salary * 0.05)
}
console.log(salary);
// 2
function count(salary, num) {
if (num === 0) {
return salary;
} else {
return count(salary + parseInt(salary * 0.05), num - 1);
}
}
console.log(count(10000, 50));
</script>
</body>
</html>
var year = 50;
// 定义函数mysal
function mysal(year) {
if (year <= 1) return 10000;
else return mysal(year - 1) * 1.05;
}
console.log(mysal(year)); //调用函数mysal
console.log(Math.pow(1.05, 49) * 10000); //验证