Páginas

martes, 1 de mayo de 2018

Pascal's triangle 2.0

In this version of the Pascal triangle, the structure of the numbers is centralized, neat and tidy.
The only change we did to the code was changing the <p> we used before with a <pre> and adding a <center> to all the HTML.
index.html
 <!DOCTYPE html>  
 <html>  
  <head>  
   <meta charset="utf-8">  
   <meta name="viewport" content="width=device-width">  
   <title>repl.it</title>  
  </head>  
  <body>  
   <center>  
    <h2>Pascal's triangle</h2>  
    <input type="number" id="main">  
    <button class="button" onclick="calculate()">Calculate</button>  
    <pre id="potato"></pre>  
   </center>  
   <script src="index.js"></script>  
  </body>  
 </html>  
index.js
 function calculate(){  
  window.n= document.getElementById("main").value;   
  var V=new Array(n);  
  var N=new Array(n);  
  var text='';  
  for (i=0;i<=n;i++){  
   V[i]=0;  
   N[i]=0;  
  }  
  V[1]=1; //This is the vertex  
  text+=V[1]+' '+'<br>';  
  //We travel from row 2 to n  
  for (j=2;j<=n;j++){  
   //Let's create the new vector  
   for (i=1;i<=j;i++){  
    N[i]=V[i-1]+V[i];  
    text+=N[i]+' ';  
   }  
   text+='<br>';  
   for (i=0;i<=n;i++){  
    V[i]=N[i];  
   }  
  }  
  document.getElementById("potato").innerHTML = text; //Print everything  
 }  
The result

No hay comentarios:

Publicar un comentario