Module and Closure Lab


1. Rewrite the setInterval example (the one with function delayMsg2 that prints out ‘Rudy!’) to use the module pattern. Call the module ‘rudyTimer’. I.e., varrudyTimer’= (… )() and then use the module as the event handler on the button. I.e.,

Solution:- let rudyTimer = null; function delayMsg2(){ let output = document.getElementById('output'); if(rudyTimer==null)rudyTimer = setInterval(()=> output.innerHTML = output.textContent+" Rudy!", 1000); else{ clearInterval(rudyTimer); rudyTimer = null; } }


Bank Account




Solution:- let createAccount = (function () { let accList = []; class Account { constructor(accName, deposit) { this.accName = accName; this.deposit = deposit; } } function display() { document.getElementById("textBox").value += "Account Name: "+accList.at(-1).accName+", Deposit: "+accList.at(-1).deposit+"\n"; } return { newAccount: function () { accList.push(new Account(document.getElementById('name').value, document.getElementById('amount').value)); display(); } }; })();