Question

JAVASCRIPT: To carry out a program that prints Pascal's triangle, the user must enter the number...

JAVASCRIPT: To carry out a program that prints Pascal's triangle, the user must enter the number of rows to display.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the complete code to print Pascal's Triangle while input being given by the user to print the number of rows:

<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .container {
            margin: 0 auto;
        }
 
        .block {
            display: inline-block;
            position: relative;
            border: 1px solid grey;
            text-align: center;
            font-size: .8em;
            width: 2em;
        }
 
        .divblock {
            text-align: center;
            vertical-align: middle;
        }
 
        #pascal {
            margin: 0 auto;
            text-align: center;
        }
 
        .block:nth-child(odd) {
            background: cyan;
        }
 
        .block:nth-child(even) {
            background: grey;
        }
    </style>
    <script>
        function printPascalTriangle(){
            document.getElementById("pascal").innerHTML = "";
            var rows = document.getElementById("numRows").value;
            var arr = generatePascal(+rows);
            for(var i=0;i<arr.length;i++){
                var div = document.createElement('div');
                div.className ="divblock"
                for(var j=0;j<arr[i].length;j++){
                    var span = document.createElement('span');
                    span.innerHTML=arr[i][j];
                    span.className ="block";
                    div.appendChild(span);
                }
                document.getElementById("pascal").appendChild(div);
            }
        }
        function generatePascal(n){
            var arr = [];
            var tmp;
            for(var i=0;i<n;i++){
                arr[i]=[];
                for(var j=0; j<=i; j++){
                    if(j==i){
                        arr[i].push(1);
                    }else{
                        tmp = (!!arr[i-1][j-1]?arr[i-1][j-1]:0)+(!!arr[i-1][j]?arr[i-1][j]:0);
                        arr[i].push(tmp);
                    }
                }
            }
            return arr;
        }
    </script>
</head>
 
<body>
    <div class="container">
        <input type="text" placeholder="Enter number of rows for pascal Triangle" style="width:80%" id="numRows">
        <input type="button" " value="Go " name="Go " onClick="printPascalTriangle() ">
        <div id="pascal"></div>
    </div>
</body>
</html>

Snapshots of Output:
1) Taking Input: As you can see on the console (right side), it is asking the user to input number of lines to be printed.
L.ll Result Enter number of rows for pascal Triangle GO } Preview > Share index.htm 1 <!DOCTYPE html> 2. <html lang=en> 3 4

2) Printing 7 lines for triangle:
I.lı Result 7 Go 1 1 1 2 1 3 زرا 1 1 4 6 4 1 5 10 10 5 1 } 6 15 20 15 6 1 Preview > Share index.htm 1 <!DOCTYPE html> 2 <html

3) Printing 10 lines for triangle:
I.ll Result 10 Gc 1 1 1 1 3 3 1 4 6 4 1 5 5 10 10 15 20 15 1 6 6 1 1 1 1 8 7 21 35 35 21 7 28 56 70 56 28 36 84 126 126 84 36

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

I have uploaded the code as per your requirement.

Please give a thumbs-up to this answer, it'll be a great help for me.

Thank you.

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Add a comment
Know the answer?
Add Answer to:
JAVASCRIPT: To carry out a program that prints Pascal's triangle, the user must enter the number...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT