Javascript basics

@Fabrice @ivga80

This is the 1st assignment.
Firstly console.log() is not working for me in the html script. It works on the browser console.
Then I switched over to document.write(). It is working, but I am not able to successfully make use of the new line ā€œ\nā€. Please see my code. Where did I go wrong?

  <script>
      var num_rows = 7;
      var toPrint ="#";
      for (var row = 0; row < num_rows; row++) {
        document.write(toPrint + "\n");
       //  <br>
        toPrint += "#";

      }

    </script>

The output appers as a single line in my case.

Edit by @gabba: You can use the Preformatted text to display code on your post :slight_smile:

1 Like

If you are rendering your javascript code into an html page you can use

        document.write("<br>");

this code display your # in the page

     var num_rows = 7;
      var toPrint ="#";
      for (var row = 0; row < num_rows; row++) {
        document.write(toPrint);
        document.write("<br>");
        toPrint += "#";

      }

However if you use console log it also works but your # will be display in the console

     var num_rows = 7;
      var toPrint ="#";
      for (var row = 0; row < num_rows; row++) {
        console.log(toPrint);
        console.log("\n");
        toPrint += "#";
      }
2 Likes