Advertisement
CLooker

crappy pascal's triangle

Nov 9th, 2018
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // crap
  2. const printPT = height => {
  3.   const getPT = () => {
  4.     let PT = [
  5.       ' 1 ',
  6.       '1 1'
  7.     ];
  8.  
  9.     if (height <= 2) return PT;
  10.  
  11.     for (let i = 0; i < height - 2; i++) {
  12.       let width = PT[PT.length - 1].length + 2;
  13.       let row = '1';
  14.       for (let j = 1; j < width; j++) {        
  15.         if (j % 2 === 0) {
  16.           row += '1';
  17.         } else {
  18.           row += ' ';
  19.         }
  20.       }
  21.       PT.push(row);
  22.     }
  23.  
  24.     PT.forEach((row, i) => {
  25.       if (i === PT.length - 1) return;
  26.       const targetLength = PT[PT.length - 1].length;
  27.       const length = row.length;
  28.       const diff = targetLength - length;
  29.       let add = '';
  30.       for (let j = 1; j <= diff/2; j++) {
  31.         add += ' ';
  32.       }
  33.       PT[i] = add.concat(row);
  34.     });
  35.  
  36.     PT.forEach((row, i) => {
  37.       if (i < 2) return;
  38.  
  39.       const prevRow = PT[i - 1];
  40.       [...row].forEach((char, j) => {
  41.         if (char === ' ' || j === 0 || j === row.length - 1) return;
  42.         const hasTopX = parseInt(prevRow[j - 1]);
  43.         const hasTopY = parseInt(prevRow[j + 1]);
  44.         if (Number.isInteger(hasTopX) && Number.isInteger(hasTopY)) {
  45.           const sum = hasTopX + hasTopY;
  46.           PT[i] = [...PT[i]]
  47.           PT[i][j] = sum;
  48.         }
  49.       })
  50.     });
  51.  
  52.     return PT;
  53.   }
  54.  
  55.   getPT().forEach(row => console.log([...row].join('')));
  56. }
  57.  
  58. printPT(10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement