Closure javascript

//Why is this code not working?

function stopWatch() {
  let startTime = Date.now();
function getDelay() {
   let elapsedTime = Date.now() — startTime;
          alert(elapsedTime);
        }
        return getDelay;
      }
      let timer = stopWatch();
    
      for (let i = 0; i < 1000000; i++) {
        let foo = Math.random() * 10000;
      }
timer();

change to…
let elapsedTime = Date.now() — startTime;
Explenation…theres something odd about the minus sign.

And dont forget Performance: now() method - Web APIs | MDN

2 Likes

Thank you

1 Like

There were 2 cons without a space

Thank you

function stopWatch() {
let startTime = new Date();
function getDelay() {
let elapsedTime = new Date() - startTime;
alert(elapsedTime);
}
return getDelay;
}
let timer = stopWatch();
for (let i = 0; i < 1000000; i++) {
let foo = Math.random() * 10000;
}
timer();

Welcome to the forums @Kenny_Fourfox! Just for my own curiosity, what code editor are you using?

VS code

Thanks! In VS Code, when you have (for example) an incorrect character, you’ll see the warnings appear in your code editor as a squiggly underline or in the console when you click on the Warning/Error icons in the bottom left corner of the window:

Your browser DevTools Console will also show you any errors as well :slight_smile:

Also if your using VS Code you can reformat the document by selecting Format Document from the context menu. This will do all the nice indenting for your code…I had problems reading this at first coz the formatting was really throwing me off.