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 JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Using For Loops with Arrays

Durdona Abdusamikovna
Durdona Abdusamikovna
1,553 Points

I removed a function print(message) now my browser opens printer to print :)

Hello,

I was following a lecture and reviewing playlist.js it has a script below

          var playList = [
              'I Did It My Way',
              'Respect',
              'Imagine',
              'Born to Run',
              'Louie Louie',
              'Maybellene'
            ];

            function print(message) {
              document.write(message);
            }

if I remove function print (message) {.....} and run the code my browser opens printer ... I would like to know why ? Is it one of the JavaScript default functions ?

Marcus Parsons

7 Answers

Right, so instead you should just use:

  var playList = [
              'I Did It My Way',
              'Respect',
              'Imagine',
              'Born to Run',
              'Louie Louie',
              'Maybellene'
            ];

            function printList(list) {
                var listHTML = '<ol>';
                for (var i = 0; i < list.length; i += 1){
                    listHTML += '<li>' + list[i] + '</li>';
                }
                listHTML += '</ol>';
                 //Changed from print(listHTML) to document.write(listHTML);
                //Got rid of print() function because it's unnecessary
                document.write(listHTML);
            }
            printList(playList);
Durdona Abdusamikovna
Durdona Abdusamikovna
1,553 Points

Yeah that's what I did and that's why I start seeing printer function...

I removed it because I don't see connection with actual code

No, you won't see the print function this way. Carefully examine what I did. In fact, copy and paste my code into the console. There is no "print()" function being used at all here because it's completely unnecessary.

What your print function was doing was just making a 2nd function to pass in data to document.write() when you can skip all of that and just pass in data to document.write(). It's much faster to just say "document.write('Hi!')" than to make a function called print and say "print('Hi!')" which sends that to "document.write('Hi!')". See what I mean?

Hey Durdona!

What browser are you using to run this code?

Durdona Abdusamikovna
Durdona Abdusamikovna
1,553 Points

Sorry forgot to say Hi :)

I don't see that function being related to the function printList () ... I linked playlist.js and my .js file to the HTML and don't know what causing it ... tried different things

The print() command is available in all browsers. I actually thought it was only Safari, but I learned something new. If you don't overwrite the print function, it will bring up a print dialogue for the current page.

In this case, you should just use document.write() anyways whenever you are making print() calls, as it is faster than creating a print function.

Durdona Abdusamikovna
Durdona Abdusamikovna
1,553 Points

So I was right it is one of the JavaScript functions cool :) Can you tell me why it opens print function when I remove it from the playlist.js ?

When you write in your own print function, it overwrites the browser's print function. So, when you don't have a custom print function, it uses the browser's default print function, which is to print the page.

But, you should just use document.write() instead of print() anyways.

Durdona Abdusamikovna
Durdona Abdusamikovna
1,553 Points

Let me put the code so it will be easier to see

        var playList = [
              'I Did It My Way',
              'Respect',
              'Imagine',
              'Born to Run',
              'Louie Louie',
              'Maybellene'
            ];

            function print(message) {
              document.write(message);
            }

            function printList(list) {
                var listHTML = '<ol>';
                for (var i = 0; i < list.length; i += 1){
                    listHTML += '<li>' + list[i] + '</li>';
                }
                listHTML += '</ol>';
                print(listHTML);
            }
            printList(playList);
Durdona Abdusamikovna
Durdona Abdusamikovna
1,553 Points

inside braces I removed 'message'now I don't see my order list ...

Durdona Abdusamikovna
Durdona Abdusamikovna
1,553 Points

Oh I see you replaced

    print(listHTML);

with

 document.write(listHTML);

Cool I learned smth. again :) Yaaaaay :)

You're very welcome :P