Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Tadjiev Codes
Tadjiev Codes
9,626 Points

Hi peeps, little help need with sth in JavaScript

Hi peeps, little help need with sth in JavaScript
1) Push the name of the caller into the “callerLog” array 2) When the counter exceeds 10, shutdown all the timers using the “clearInterval” method.

I can't make both of these tasks work as I don't understand how to get the Timer1 or Timer2 that's being pushed through setinterval. And, in clearInterval to shut down the timers I'm giving a wrong argument so in both cases I just need to correct the parameters or argument values in parentheses () on line 22 and line 37. Help would be really appreciated)))

        var timers = [];


        // IIFE kind of Function
        var log = (function (outputFunc) {
            var counter = 0;
            var callerLog = [];
            var dateTime = [];

            // return inner anonymous function
            // your code goes here
            return function(timer) {
            //step1   Increment the counter by 1
              counter++;
              console.log(counter);
            //step 2 Push the name of the caller into the “callerLog” array
             var holdCallerLog = callerLog.push(timers); 
              console.log(holdCallerLog);
             // step 3 Push the current date/time into the dateTime array (use new Date() to retrieve thedate/time).
              var dateT = dateTime.push(new Date()); 
               console.log(dateT);
             // step 4 Create an output string by concatenating the counter, the last entry in “callerLog” andthe last entry in “dateTime.”
             var outputMessage = `Counter ${counter} ${ callerLog.slice(-1)[0] } ${ dateTime.slice(-1)[0] }`;   

              // or arr.slice(-1).pop() in order to remove the last item of the array

            // step 5 Output the string using the designated output function. Note you may not use“printFunc” directly.
            printFunc(outputMessage);

             // step 6 When the counter exceeds 10, shutdown all the timers using the “clearInterval”method.
             if (counter >= 10){
             clearInterval(timers);
             }

            }

        })(printFunc);

        function printFunc(output) {
            document.write(output + "<br>");
        }

        function startMeUp() {
            // add each of the timer references to the timers array
            // as part of invoking the log function following each interval
            timers.push(setInterval("log('Timer1')", 1000));
            timers.push(setInterval("log('Timer2')", 1200));
            timers.push(setInterval("log('Timer3')", 1700));
        }

        </script>
    </head>
    <body onload="startMeUp();">

    </body>
</html>

Link for the workspace: https://w.trhou.se/8876lca4ti