我想我的语法错误,我需要[通用能量饮料]。它返回相同的值。
var tal1 = antalAr(1000000,1000);
document.writeln("A loan of 1000000 SEK is paid after " +
Math.ceil(answer) + " years if the installment is 1000kr/mån.");
var tal2 = antalAr(1500000,500);
document.writeln("A loan of 1500000 SEK is paid after " +
Math.ceil(answer) + " years if the installment is 500kr/mån.");
function antalAr(lan, amortering)
{
amoyear = amortering * 12;
for( var answer = 1; amoyear * answer <= loan; answer++){}
return answer;
}
答案 0 :(得分:3)
回答仅在antalAr()的本地函数范围内声明。你不能在函数之外使用它。
试试这个:
var tal1 = antalAr(1000000,1000);
document.writeln("A loan of 1000000 SEK is paid after " +
Math.ceil(tal1) + " years if the installment is 1000kr/mån.");
var tal2 = antalAr(1500000,500);
document.writeln("A loan of 1500000 SEK is paid after " +
Math.ceil(tal2) + " years if the installment is 500kr/mån.");
function antalAr(loan, amortering)
{
amoyear = amortering * 12;
for( var answer = 1; amoyear * answer <= loan; answer++){}
return answer;
}
编辑:哦,也是一个错字
答案 1 :(得分:2)
看起来像一个简单的拼写错误:
function antalAr(lan, amortering)
应该是
function antalAr(loan, amortering)
此外,answer
应在for循环之外声明:
function antalAr(loan, amortering) {
var answer, amoyear = amortering * 12;
for(answer = 1; amoyear * answer <= loan; answer++){}
return answer;
}