JSFiddle - React, Tailwind, and code Playground
by Yurii Predborskyi
JavaScript
let getRow = function(rowIndex) {
let row = [1];
for (let i = 0; i < rowIndex; ++i) {
row[row.length] = row[i] * (rowIndex - i) / (i + 1);
}
return row;
};
let tests = [
{ rowIndex: 0, ans: [1] },
{ rowIndex: 1, ans: [1, 1] },
{ rowIndex: 2, ans: [1, 2, 1] },
{ rowIndex: 3, ans: [1, 3, 3, 1] },
{ rowIndex: 4, ans: [1, 4, 6, 4, 1] },
];
tests.forEach(test => {
let res = getRow(test.rowIndex);
console.log('expected:', test.ans, '| calculated:', res);
});