aboutsummaryrefslogtreecommitdiff
path: root/challenge-003/zapwai/javascript/ch-2.js
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-003/zapwai/javascript/ch-2.js')
-rw-r--r--challenge-003/zapwai/javascript/ch-2.js28
1 files changed, 28 insertions, 0 deletions
diff --git a/challenge-003/zapwai/javascript/ch-2.js b/challenge-003/zapwai/javascript/ch-2.js
new file mode 100644
index 0000000000..099c797138
--- /dev/null
+++ b/challenge-003/zapwai/javascript/ch-2.js
@@ -0,0 +1,28 @@
+let n = prompt("Please enter # of rows: ");
+m = [];
+m.push([1]);
+m.push([1, 1]);
+for (let i = 2; i < n; i++) {
+ row = [];
+ for (let j = 0; j <= i; j++) {
+ if ( (j == 0) || (j == i) ) {
+ row.push(1);
+ } else {
+ row.push(m[i-1][j-1] + m[i-1][j]);
+ }
+ }
+ m.push(row);
+}
+console.log(m);
+for (let i = 0; i < n; i++) {
+ let space = "";
+ for (let sp = 0; sp < n - i; sp++) {
+ space += "&nbsp";
+ }
+ document.write(space);
+ for (let j = 0; j <= i; j++) {
+ document.write(`${m[i][j]} `);
+ }
+ document.write("<br>");
+}
+