Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // crap
- const printPT = height => {
- const getPT = () => {
- let PT = [
- ' 1 ',
- '1 1'
- ];
- if (height <= 2) return PT;
- for (let i = 0; i < height - 2; i++) {
- let width = PT[PT.length - 1].length + 2;
- let row = '1';
- for (let j = 1; j < width; j++) {
- if (j % 2 === 0) {
- row += '1';
- } else {
- row += ' ';
- }
- }
- PT.push(row);
- }
- PT.forEach((row, i) => {
- if (i === PT.length - 1) return;
- const targetLength = PT[PT.length - 1].length;
- const length = row.length;
- const diff = targetLength - length;
- let add = '';
- for (let j = 1; j <= diff/2; j++) {
- add += ' ';
- }
- PT[i] = add.concat(row);
- });
- PT.forEach((row, i) => {
- if (i < 2) return;
- const prevRow = PT[i - 1];
- [...row].forEach((char, j) => {
- if (char === ' ' || j === 0 || j === row.length - 1) return;
- const hasTopX = parseInt(prevRow[j - 1]);
- const hasTopY = parseInt(prevRow[j + 1]);
- if (Number.isInteger(hasTopX) && Number.isInteger(hasTopY)) {
- const sum = hasTopX + hasTopY;
- PT[i] = [...PT[i]]
- PT[i][j] = sum;
- }
- })
- });
- return PT;
- }
- getPT().forEach(row => console.log([...row].join('')));
- }
- printPT(10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement