Displaying Alternative Positive and Negative Numbers in JavaScript

  1. <!DOCTYPE html>  
  2. <html>  
  3.     <body>  
  4.         <p id="displayseries"></p>  
  5.         <input type = "text" id ="UserInput" />  
  6.         <button type="button" onClick="MakeSeries()"> Enter number upto which series has to be made.</button>  
  7.         <script>  
  8.             function MakeSeries(){  
  9.             var x = document.getElementById("UserInput").value;  
  10.             var text = "";  
  11.             var i;var j = -2;  
  12.             for (i = 1; i < x; i++) {  
  13.                text += "+" + i + "  
  14.              <br>";  
  15.               i = i+1;  
  16.             while (j > -x) {  
  17.               text +=  j + "  
  18.                 <br>";  
  19.                 j = j-2;  
  20.                 break;  
  21.             }}  
  22.             document.getElementById("displayseries").innerHTML = text;  
  23.          }  
  24.         </script>  
  25.     </body>  
  26. </html>