Sample solutions are contained in the following 3 html files. Viewing the source of each will allow you to study the JavaScript code.
html
lab-sol1.html
lab-sol2-1.html
lab-sol2-2.html
lab-sol3.html