August 2016
Intermediate to advanced
635 pages
14h 5m
English
While imperative code tells the machine, step-by-step, what it needs to do to solve the problem, functional programming instead seeks to describe the problem mathematically so that the machine can do the rest.
With a more functional approach, the same application can be written as follows:
// separate the data and logic from the interface var printPrice = function(price, label) { var node = document.createElement("li"); var textnode = document.createTextNode(label+' price: $'+price); node.appendChild(textnode); document.getElementById('products 2').appendChild(node); } // create function objects for each type of coffee var columbian = function(){ this.name = 'columbian'; this.basePrice = 5; }; var frenchRoast = function(){ ...