Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson

Already subscribed? Sign In

Autoplay

    Using console.count to Count Events

    mykola bilokonskymykola bilokonsky

    Learn how to user console.count in order to log out how many times a given thing has happened.

    chrome-devtoolsChrome DevTools
    javascriptJavaScript
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    00:00 Have you ever needed to count how many times something happened when you're debugging something? Like let's say for instance we go for our i=zero, i<100, i++, and var num=math.random(100).

    00:20 Let's say if num>50, console.log(number was greater than 50) else console.log number was not...let's just say it was less than 50.

    00:46 If you log the same thing more than once, Chrome is going to give you a count of how many times that particular message was repeated. But this sucks, right? What we want to do now is, crap, OK, we got to keep a counter. We're going to say var num>50=0var num<50=0We've introduced this additional mutable state. Then we can say here >50 and we can say ++num>50. Here we can say <50.

    01:31 Now when we run this we've got this running tally. We can see 54 times it was greater than 50, 46 it was...whatever. This is all wrong. I'm doing this just to show you that there is a better way and we don't have to introduce these counters. We just use one of the many methods that they added to the console to make our lives easier. We can say count -- console.count>50. Here we can say console.count<50. We run this and that takes care of that for us automatically.

    Discuss

    Discuss