Skip to content

Commit 554ec2d

Browse files
committed
add the lesson two page 5 task, work in progress
1 parent 979e7b9 commit 554ec2d

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

nathansununiversity_02.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// http://nathansuniversity.com/music.html
2+
// this file supposed to work with node.js
3+
//
4+
// following compile function is from last lesson
5+
var compile = function (expr) {
6+
7+
var result = [];
8+
9+
(function endTime(time, expr, ref) {
10+
// your code here
11+
(function(expr) {
12+
if (expr.tag === 'note') {
13+
expr.start = ref;
14+
result.push(expr);
15+
16+
time += expr.dur;
17+
} else if (expr.tag === 'par') {
18+
var endLeft = endTime(0, expr.left, time);
19+
var endRight = endTime(0, expr.right, time);
20+
time += endLeft > endRight ? endLeft : endRight;
21+
} else {
22+
arguments.callee(expr.left);
23+
arguments.callee(expr.right);
24+
}
25+
})(expr);
26+
27+
return time;
28+
})(0, expr, 0);
29+
30+
return result;
31+
};
32+
33+
// testing code
34+
var melody_mus =
35+
{ tag: 'seq',
36+
left:
37+
{ tag: 'seq',
38+
left: { tag: 'note', pitch: 'a4', dur: 250 },
39+
right: { tag: 'note', pitch: 'b4', dur: 250 }
40+
},
41+
right:
42+
{ tag: 'seq',
43+
left: { tag: 'note', pitch: 'c4', dur: 500 },
44+
right: { tag: 'note', pitch: 'd4', dur: 500 }
45+
}
46+
};
47+
48+
console.log(melody_mus);
49+
console.log(compile(melody_mus));

0 commit comments

Comments
 (0)