user592638
user592638

Reputation:

return statement

I think I have missunderstod the syntax, I need a [generic energy drink]. It returns the same value.

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;
        }

Upvotes: 1

Views: 180

Answers (2)

Stephen
Stephen

Reputation: 18964

Looks like a simple typo:

function antalAr(lan, amortering)

should be

function antalAr(loan, amortering)

Also, answer should be declared outside the for-loop:

function antalAr(loan, amortering) {
    var answer, amoyear = amortering * 12;
    for(answer = 1; amoyear * answer <= loan; answer++){}
    return answer;
}

Upvotes: 2

goto-bus-stop
goto-bus-stop

Reputation: 11824

answer is only declared in the local function scope in antalAr(). You can't use it outside the function.

try this:

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;
        }

edit: oh, a typo as well

Upvotes: 3

Related Questions