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
  • Create a Pascal's Triangle in Python or Atom Javascript the program allows the user to input...

    Create a Pascal's Triangle in Python or Atom Javascript the program allows the user to input any # and creates the triangle from it

  • Please solve in C++ Pascal's Triangle Videos [edit] write a C++ that asks the user to...

    Please solve in C++ Pascal's Triangle Videos [edit] write a C++ that asks the user to enter the number of rows (N) and then prints the first N rows of Pascal's Triangle, che row per line. Your program should echo print N. Good News [edit] The best way to construct Pascal's Triangle is to use a nested loop with repeated calls to the combination already written for a previous programming assignment, Just #include "myFunctions.h" and use the combination function from...

  • Write a program which prompts the user to enter a number of rows, and prints a...

    Write a program which prompts the user to enter a number of rows, and prints a hollow square star pattern with 2 diagonals. Note: you can assume that the integer will always be > 1 and will be an odd number. For example: Input Result 5 Enter number of rows: 5 ***** ** ** * * * ** ** ***** 9 Enter number of rows: 9 ** ** ** * * * * * * * * * * **...

  • complete a program so that it asks the user to enter a number and then prints...

    complete a program so that it asks the user to enter a number and then prints out the user's number, 2x the user's number, and 10x the user's number

  • use scheme program The following pattern of numbers is called Pascal's triangle. The numbers at the...

    use scheme program The following pattern of numbers is called Pascal's triangle. The numbers at the edge of the triangle are all 1, and each number inside the triangle is the sum of the two numbers above it. (pascals 0 0) → 1 (pascals 2 0) → 1 (pascals 2 1) → 2 (pascals 4 2) → 6 (printTriangle 5) 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 [5 marks] Write a procedure...

  • Write a program that asks the user for a negative number, then prints out the product...

    Write a program that asks the user for a negative number, then prints out the product of all the numbers from -1 to that number Enter a negative number: -6 720 (The answer is 720 because-1x-2x-3x-4x-5 x-6 720).

  • Write a javascript program that asks the user to enter five test scores. The program should...

    Write a javascript program that asks the user to enter five test scores. The program should display a letter grade for each score and the average test score. Write the following methods in the program: calcAverage—This method should accept five test scores as arguments and return the average of the scores. determineGrade—This method should accept a test score as an argument and return a letter grade for the score,

  • Write a program that asks the user to enter a number ofseconds.• There are...

    Starting Out with C++ (9th Edition)  Chapter 4, Problem 7PCWrite a program that asks the user to enter a number of seconds.• There are 86400 seconds in a day. If the number of seconds entered by the user is greater than or equal to 86400, the program should display the number of days in that many seconds.• There are 3600 seconds in an hour. If the number of seconds entered by the user is less than 86400, but is greater...

  • Implement a C++ program that lets the user enter a number and then prints the result...

    Implement a C++ program that lets the user enter a number and then prints the result calculated by the following function. Write a recursive function that computes n * n/3 * n/9 * n/27 * n/81... where n is a non-negative integer, n/x is integer division and the quotient is always a multiple of 3 from the previous quotient. The sequence repeats as long as n/x > 0. For example: 27: 27 * 9 * 3 * 1 = 729...

  • please solve using python thank you! Write a program which prompts the user to enter a...

    please solve using python thank you! Write a program which prompts the user to enter a number of rows, and prints a hollow square star pattern with 2 diagonals. Note: you can assume that the integer will always be > 1 and will be an odd number. For example: Input Result 5 Enter number of rows: 5 ***** ** ** * * * ** ** ***** 9 Enter number of rows: 9 ** ** ** * * * * *...

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