Search code examples
htmlpre

<PRE> TAG doesn't work?


Here, what I am doing is printing my javascript code by wrapping between "pre" tags like this :

<pre>
    var sum = function(toSum) {
    var j = 0;
    for(var i=0; i<toSum.length; i++) {
        j = j + toSum[i];   
    }

    console.log("The sum of array is " + j);
};

sum([1,2,3,4]);

var multiply = function(toMultiply) {
    var j = 1;
    for(var i=0; i<toMultiply.length; i++) {
        j = j * toMultiply[i];  
    }

    console.log("The multiplication of array is " + j);
};

multiply([1,2,3,4]);
</pre>

But what I am actual getting is this :

var sum = function(toSum) {
var j = 0;
for(var i=0; i

Why is that so? How can I get "pre" tag working?


Solution

  • You should use &lt; instead of <. Similarly, use &gt; for > and &amp; for & when displaying in HTML.

    Your for loop will be like:

    for(var i=0; i&lt;toSum.length; i++) {
    ...
    }