-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpascalstriangle.html
62 lines (52 loc) · 1.72 KB
/
pascalstriangle.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pascals Triangle</title>
<style></style>
<meta />
<link />
<link rel="icon" href="">
<link rel="stylesheet" type="text/css" href="css/style.css">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Zen+Dots&display=swap" rel="stylesheet">
</head>
<a id="back" href="./index.html">BACK</a>
<body>
<h1></h1>
<p>Pascal's triangle is a triangular array constructed by summing adjacent elements in preceding rows. Pascal's triangle contains the values of the binomial coefficient. It is named after the 17th century French mathematician, Blaise Pascal (1623 - 1662).</p>
<label for="rows">Number of Rows:</label><br>
<input class="input" type="number" id="input" name="rows" value="5" onChange='generatebtn()'><br>
<button class='button' onClick='generatebtn()' > Generate </button>
<div id='parent'></div>
<pre>
<code>
var generate = function(numRows) {
//make array of length numRows
let rowsArray = [];
// make the rows - for loop - each iteration appends a row to rowsArray
for (i = 0; i <= numRows-1; i++ ){
let newRow = [i];
newRow[0] = 1;
newRow[i] = 1;
rowsArray[i] = newRow;
};
for (j = 0; j <= numRows-1; j++){
// calculate each number based on the previous row - first and last are always 1
for (k = 0; k <= j; k++){
//skip first iteration
if (j >= 1){
// skip first and last numbers
if (k != 0 && k != j){
rowsArray[j][k] = rowsArray[j-1][k] + rowsArray[j-1][k-1];
}
}
};
};
return rowsArray;
};
</code>
</pre>
</body>
<script src=./js/pascalstriangle.js> </script>