13. JavaScript Programming Paradigms
Activity 13.01: Creating a Calculator App
Solution
- Create an empty file and call it
procedural.js
. - Initialize an array that will maintain the history of function calls:
lethistoryList = [];
- Now, create simple addition, subtraction, multiplication, division, and power functions:
procedural.js
3Â function add(a, b){ 4Â historyList.push(['ADD', a, b]); 5Â return a+b; 6Â } 7Â 8Â function subtract(a, b){ 9Â historyList.push(['SUB', a, b]); 10Â return a-b; 11Â } 12Â 13Â function multiply(a,b){ 14Â historyList.push(['MUL', a, b]); 15Â return a*b; 16Â }
The full code is available at: https://packt.live/2Xf6kHk
- Create a
history
function, which will maintain the history of function calls:function history(){ Â historyList.map((command, index)=>{ Â Â console.log(index+1+'.', command.join(' ')); Â }) }Call all...