-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyramid.js
35 lines (32 loc) · 921 Bytes
/
Pyramid.js
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
function solution1(input) {
const center = input - 1;
for (let i = 1; i <= input; i++) {
row = Array(2 * input - 1).fill(" ");
for (let j = 0; j < i; j++) {
row[center + j] = row[center - j] = "#";
}
console.log(`'${row.join("")}'`)
}
}
function solution2(input) {
const center = input - 1;
for (let i = 0; i < input; i++) {
let row = "";
for (let j = 0; j < 2 * input - 1; j++) {
if(j >= center - i && j <= center + i) row += "#";
else row += " ";
}
console.log(`'${row}'`)
}
}
function run() {
const inputs = [1, 2, 3, 4];
const solutionsCount = 2;
for (let s = 0; s < solutionsCount; s++) {
for (let i = 0; i < inputs.length; i++) {
const answer = eval(`solution${s + 1}(${JSON.stringify(inputs[i])})`);
console.log(`solution${s + 1} : ${inputs[i]} => ${answer}`);
}
}
}
run();