#MonthOfCode - Day 8: mathematics

My entry for the 8th day of the month of code. The theme for today is: mathematics.

It’s sunny out there and I don’t want to stay in front of my computer so this one is really simple: it generates Pascal’s triangle

Code after the break.

mathematics.jsview raw
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
(function() {

var el = document.getElementById('moc-8');

var currentLine = [1]; // first line
var lines = [];
for (var i = 0; i < 12; i++) {
lines.push(
'<div class="line" style="width:' + (currentLine.length * 35) + 'px;"><span class="cell">' +
currentLine.join('</span><span class="cell">') +
'</span></div>'
);

var nextLine = [1];
for (var j = 0; j < currentLine.length - 1; j++) {
nextLine.push(currentLine[j] + currentLine[j + 1]);
}
nextLine.push(1);

currentLine = nextLine;
}

el.innerHTML = lines.join('');

})();
mathematics.cssview raw
1
2
3
4
5
6
7
8
#moc-8 .line {
margin: 0 auto;
}

#moc-8 .cell {
display: inline-block;
width: 35px;
text-align: center;
}