May 29, 2020
Difference Between Pure Function and Impure Function in Javascript
HTML:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="style.css"> </head> <body> <div> <button>Click Me</button> <h1 id="total">Total: 0</h1> <h1 id="xvar">X: 0</h1> </div> <script src="script.js"></script> </body> </html>
CSS:
div { display: flex; flex-direction: column; align-items: center; }
Javascript:
let btnCheck = document.querySelector('button'); let total = document.querySelector('#total'); let xvar = document.querySelector('#xvar'); let x = 0; btnCheck.addEventListener('click', () => { total.innerText = `Total: ${myFunc(5, 6)}`; xvar.innerText = `X: ${x}`; }); function myFunc(a, b) { x = x + a + b; return a + b; }