Skip to content Skip to sidebar Skip to footer

Product Of A Sequence (factorial) Javascript

The idea is to find numbers in a sequence.. equation pic: But the problem is pat.innerHTML doesn't multiply all previous n numbers. The idea is to create a sequence: a(2) = 1000

Solution 1:

It sounds like you just need to keep track of a persistent variable that holds the result from the last iteration, and calculate the result for the current iteration by multiplying by it. The sequence starts at 1000, so put that as the initial result:

var pat = document.getElementById("pattern");
let lastResult = 1000;
for (var n = 2; n <= 94; n++) {
  const nextResult = lastResult * ((999 - 10 * (n - 2))/(1000 - 10 * (n - 2)))
  pat.innerHTML += nextResult + '<br>';
  lastResult = nextResult;
}
<body><pid="pattern"></p></body>

Solution 2:

Best way is to make helper method for calculation. If you put another for loop then append new values just like math need's.

/*
 Formula
*/functioncalcFactoriel(arg) {

 var a = 0; // for numbersvar b = ""; // for stringfor (var x = arg; x > 1; x--) {
 
    if (a == 0) {
      a = 1000 * (999 - 10 * (x - 2))/(1000 - 10 * (x - 2))  
    } else {
      a *= 1000 * (999 - 10 * (x - 2))/(1000 - 10 * (x - 2))  
    }
    
    b = "<div class='m' >" +  a + " , </div> ";
    
 }
 
 return b;
}

var pat = document.getElementById("pattern");
var nMax = 96; 

for (var n = 2; n <= nMax; n++) {
    	pat.innerHTML += calcFactoriel(n);
}
.m {
 color: rgba(0,0,0,0.6);
 background: #629552;
 text-shadow: 2px8px6pxrgba(0,0,0,0.2), 0px -5px35pxrgba(255,255,255,0.3);
}
<body><pid="pattern"></p></body>

Post a Comment for "Product Of A Sequence (factorial) Javascript"