#MonthOfCode - Day 4: error

My entry for the 4th day of the month of code. The theme for today is: error.

This code has errors. Fix them until it’s green!

Code after the break.

error.jsview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
document.addEventListener('DOMContentLoaded', function() {

$('#moc-4 > textarea').on('keyup', function () {
var source = this.value;
function assert(ok) {
if(ok !== true) {
throw new Error('Assertion failed');
}
}
var hasErrors = false;
try {
eval(source);
} catch (e) {
hasErrors = true;
}
$(this).css('background-color', hasErrors ? '#F08C8C' : '#8CF08C');
});

});