-3

i want to add two number six times by looping & function calling. loop execute properly but function not execute properly. here the function may not called.please help. Thanks in advance

<html>
    <head>

        <script type="text/javascript">

        var i=10;
        for(j=1;j<=6;j++) {
            var x=fun(3,4);
            document.write(x);
            i++;
        }

        function fun(var i, var j) {
            var k=i+j;
            return k;
        }

        </script>
    </head>
    <body>
    </body>
</html>
5
  • You're writing your values in the head of the document and you have erroneous var in the function declaration. Commented Sep 8, 2014 at 12:06
  • document.write should almost never be used. If you don't know why, then it should never be used. The only good way to use document.write - stackoverflow.com/a/20436093/1435655 Commented Sep 8, 2014 at 12:08
  • @m59 maybe you should also explain why it should never be used or link to a resource that explains why. Commented Sep 8, 2014 at 12:10
  • @SleepDeprivedBulbasaur I disagree. That's been explained about several hundred times or more elsewhere and Google is a great tool. Commented Sep 8, 2014 at 12:11
  • Please learn to use the JavaScript console in your browser. It would quickly point where where your problem was. Commented Sep 8, 2014 at 12:21

2 Answers 2

4

You have erroneous var in the function declaration ( function fun(var i, var j) ). This is a syntax error which prevents the execution of the script.

The correct syntax is function fun (i, j).


Note that you're using document.write (hopefully for the purpose of the question). That's a tricky function, which can only be executed during the initial loading of the page, and which should generally be avoided. For testing a code, the best practice is usually to use console.log instead and open the console to see the log (you may hit F12).

Sign up to request clarification or add additional context in comments.

Comments

-1
remove var keyword from function parameter 

function fun(i,j)
    {
    var k=i+j;
    return k;

    }

1 Comment

This appears to be a game of spot-the-difference, not answer. What did you change? Why should it make a difference?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.