The idea is to find numbers in a sequence.. equation pic:
var pat = document.getElementById("pattern"); var nMax = 96; for (var n = 2; n <= 96; n++) { if (n != 96) pat.innerHTML += 1000 * (999 - 10 * (n - 2))/(1000 - 10 * (n - 2)) + ", "; else pat.innerHTML += 1000 * (999 - 10 * (n - 2))/(1000 - 10 * (n - 2)) + ",…"; }
<body> <p id="pattern"></p> </body>
But the problem is pat.innerHTML doesn't multiply all previous n numbers. The idea is to create a sequence:
a(2) = 1000 * (999 - 10 * (2 - 2))/(1000 - 10 * (2 - 2)) a(3) = 1000 * (999 - 10 * (2 - 2))/(1000 - 10 * (2 - 2)) * (999 - 10 * (3 - 2))/(1000 - 10 * (3 - 2)) a(4) = a(3) * (999 - 10 * (4 - 2))/(1000 - 10 * (4 - 2))
etc.. How do I do that? (See pic for the equation in proper math notation.)
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> <p id="pattern"></p> </body>
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 */ function calcFactoriel(arg) { var a = 0; // for numbers var b = ""; // for string for (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: 2px 8px 6px rgba(0,0,0,0.2), 0px -5px 35px rgba(255,255,255,0.3); }
<body> <p id="pattern"></p> </body>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.