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...