Javascript 每年计算2%的贴现率
我需要写一段代码,请求合同年数的值。然后使用for循环计算每年2%的折扣系数,即如果是一年期合同,价格将是总价的98%,如果是两年期合同,价格将是总价的96%,依此类推 我似乎有点卡住了,不确定自己是否完全理解了他们的要求 以下是我已经做过的:Javascript 每年计算2%的贴现率,javascript,Javascript,我需要写一段代码,请求合同年数的值。然后使用for循环计算每年2%的折扣系数,即如果是一年期合同,价格将是总价的98%,如果是两年期合同,价格将是总价的96%,依此类推 我似乎有点卡住了,不确定自己是否完全理解了他们的要求 以下是我已经做过的: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transition//EN" "http://www.w3.org/TR/xhtml/DTD/xhtml1-transitional.dtd"> &l
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transition//EN" "http://www.w3.org/TR/xhtml/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<script type = "text/javascript">
var stringVariable = prompt ("Enter the number of people")
var numberVariable
var loopCounter = prompt ("How many years?");
var numberCount = new Array(100/2);
if (stringVariable <= 30) {
numberVariable = 15*stringVariable;
}
else if (stringVariable> 30 && stringVariable<60) {
numberVariable = 12*stringVariable;
}
else if (stringVariable>60) {
numberVariable =12*stringVariable;
}
alert ("Total cost is: $" + numberVariable);
for (loopCounter = 0; loopCounter <= 4; loopCounter++)
{
document.write("Total discount $" + loopCounter - numberCount[loopCounter] + "<br />");
}
alert ("Total cost is: $" + numberVariable - numberCount);
</script>
</body>
</html>
var stringVariable=prompt(“输入人数”)
变量
var loopCounter=prompt(“多少年?”);
var numberCount=新数组(100/2);
if(stringVariable 30和stringVariable 60){
numberVariable=12*stringVariable;
}
警报(“总成本为:$”+数字变量);
对于(loopCounter=0;loopCounter您的代码似乎在一些地方存在根本性的缺陷,尤其是您的变量名
下面是我如何解决这个问题的:
// parseInt() converts strings into numbers. 10 is the radix.
var num_people = parseInt(prompt('Enter the number of people'), 10);
var num_years = parseInt(prompt('How many years?'), 10);
// Initialize your variables.
var cost = 0;
var discount = 1.00;
// Your if condition was a bit odd. The second part of it would be
// executed no matter what, so instead of using else if, use an
// else block
if (num_people <= 30) {
cost = 15 * num_people;
} else {
cost = 12 * num_people;
}
alert('Total cost is: $' + cost);
// Here is a for loop. i, j, k, ... are usually
// used as the counter variables
for (var i = 0; i < num_years; i++) {
// Multiplying by 0.98 takes 2% off of the total each time.
discount *= 1.00 - 0.02;
// You fill the rest of this stuff in
document.write('Total discount $' + ... + '<br />');
}
// And this stuff
alert('Total cost is: $' + ...);
//parseInt()将字符串转换为数字。10是基数。
var num_people=parseInt(提示(“输入人数”),10);
var num_years=parseInt(提示(‘多少年?’),10);
//初始化变量。
风险价值成本=0;
风险价值折扣=1.00;
//你的if条件有点奇怪,第二部分是
//无论发生什么情况都执行,因此不要使用else if,而是使用
//else块
如果(num_people您的代码似乎在一些地方存在根本性的缺陷,尤其是您的变量名
下面是我如何解决这个问题的:
// parseInt() converts strings into numbers. 10 is the radix.
var num_people = parseInt(prompt('Enter the number of people'), 10);
var num_years = parseInt(prompt('How many years?'), 10);
// Initialize your variables.
var cost = 0;
var discount = 1.00;
// Your if condition was a bit odd. The second part of it would be
// executed no matter what, so instead of using else if, use an
// else block
if (num_people <= 30) {
cost = 15 * num_people;
} else {
cost = 12 * num_people;
}
alert('Total cost is: $' + cost);
// Here is a for loop. i, j, k, ... are usually
// used as the counter variables
for (var i = 0; i < num_years; i++) {
// Multiplying by 0.98 takes 2% off of the total each time.
discount *= 1.00 - 0.02;
// You fill the rest of this stuff in
document.write('Total discount $' + ... + '<br />');
}
// And this stuff
alert('Total cost is: $' + ...);
//parseInt()将字符串转换为数字。10是基数。
var num_people=parseInt(提示(“输入人数”),10);
var num_years=parseInt(提示(‘多少年?’),10);
//初始化变量。
风险价值成本=0;
风险价值折扣=1.00;
//你的if条件有点奇怪,第二部分是
//无论发生什么情况都执行,因此不要使用else if,而是使用
//else块
如果(num_people两年后,价格将不会是原版的96%。它将是原版的96.04%。无论你在卖什么,给我注册50年。两年后,价格将不会是原版的96%。它将是原版的96.04%。无论你在卖什么,请给我注册50年。顺便说一句,为什么不使用discount*=0.98;
?:)@Nick:我只是明确显示了0.98
的来源。你发这篇文章真不错。顺便说一句,为什么不直接使用折扣*=0.98;
?:)@Nick:我只是明确显示了0.98
的来源。