Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Dec 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question JavaScript Program - Need your help!!

    So I'm new at programming (just so you know) and I have a problem with my program. I want to create a function that print Pascal's triangle (centered). I have few problems with the spacing and general formats...

    Thanks for your help in advance
    Last edited by 123jo; 12-02-2012 at 02:57 PM. Reason: My code is modified below

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,627
    Thanks
    0
    Thanked 648 Times in 638 Posts
    JavaScript doesn't have a print function so for the print statements to work you'd need to create that function.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    123jo (12-01-2012)

  • #3
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    224
    Thanks
    0
    Thanked 32 Times in 30 Posts
    Try simply this
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="generator" content="PSPad editor, www.pspad.com">
    <title></title>
    <style type="text/css">
    body{font-family:courier,monspace;text-align:center;}
    p{margin:3px;}
    </style>
    </head>
    <body>
    <h1>Pascal's Triangle</h1>
    <div id="pge"></div>
    <script type="text/javascript">
    function co(n,p) {
        if ( n<p || n<0 || p<0 ) return 0;
        if ( !p || !n) return 1;
        return co(n-1,p-1)+co(n-1,p);
    }
    var N=0;
    function nxtLgn(){var i,str='';
    	N++;
    	for (i=0;i<N;i++) str+=co(N,i)+'&nbsp;';
    	document.getElementById('pge').innerHTML+='<p>'+str+'</p>';
    	if (N<21) setTimeout(nxtLgn,10);
    }
    nxtLgn();
    </script>
    </body>
    </html>

  • #4
    New Coder
    Join Date
    Dec 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks for your help guys.
    Last edited by 123jo; 12-01-2012 at 09:11 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •