Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
var x = function (a) { return a + a/4 - 600}
I have a function that does something.
So now my goal is to repeat this function 12 times with an argument used from previous operation;
Let's say initial ammount is 5000;
So
`x(5000) = 5650;
x(5650) =6462.5;
x(6462.) =...;
and this should be repeated 12 times;
So how can this be done in code?
`
Certainly the easiest way would be to use a for loop:
var x = function(a) { return a + a/4 - 600 },
v = 5000;
for (var i = 0; i < 12; i++) {
v = x(v);
}
console.log(v);
I'd not recommend doing something like this, but instead of a loop you could use recursion:
var x = function(42.34, 12);
----
function(double a, int maxSize)
{
a = ((a + a) / (4 - 600));
if(maxSize>0){
maxSize--;
return function(a,maxSize);
}else{
return a;
}
}
(just pseudocode)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
n = 1;
while (n < 1024) {
alert(Math.pow(n, 2));
n = n + 2;
}
How can I make this display the powers of 2 from 2^0 to 2^10 inclusive i.e. (1-1024) and end once it reaches 1024 in Javascript
I would do something like this:
for(let n = 0; n <= 10; n++) {
console.log(2 ** n)
}
This logs the answer of each loop iteration. As Pointy suggested in the comment, we have to make the n value iterate from 1 to 10, since that is the variable that generates the output, whereas the 1024 is simply the output that comes from the power values. You could also use Math.pow as follows:
for(let n = 0; n <= 10; n++) {
console.log(Math.pow(2, n))
}
Implementing Secan's suggestion, here is a function which will take any number as a parameter:
function foo(x, exp) {
for(let i = 0; i <= exp; i++) {
console.log(x ** i)
}
}
So to apply this to the original answer, we would call it like this:
foo(2, 10)
Sorry for the ambuiguity with my original question. I've figured it out-
n=0;
while (n <= 10) {
alert(Math.pow(2, n));
n = n + 1;
}
Setting n=0, n<=10 in while and changing n + 1 instead of 2 resolved my issue. Thanks for your responses!
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
This gives the output of 0 which means that something is wrong with how I call the factorial function.
function factorial(n) {
let answer = 1;
if (n === 0 || n === 1) {
return answer;
} else {
for (var i = n; i >= 1; i--) {
answer = answer * i;
}
return answer;
}
}
let computation = 0;
function compute(){
let a = 5;
let b = 6
let sum = a + b;
computation = sum + factorial(5);
}
console.log(`The value of this is + ${computation}`);
I don't see you calling compute (i.e. compute()), so computation is still as you initialized it as 0.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I would like to sum up all the elements inside the array covertedValue. Why I am getting a Nan result? whats wrong with the recursive function I have written?
function findOutlier(integers){
var covertedValue = integers.map(x => x % 2);
var total = 0;
for (i=0 ; i<covertedValue.length ; i++){
total = total + covertedValue[0] + findOutlier(integers.splice(1));
}
console.log(total);
}
findOutlier([0, 1, 2]);
because this line create a function and a private alias to itself
var factorial =
// create a variable named factorial in global scope
// it contains the function fac
function fac(n) {
// fac is a function available only in the factorial var context
return n < 2 ? // if n < 2
1 : // return 1
n * fac(n - 1); // else return n * fac(n - 1)
};
console.log(factorial(5));
// calls factorial(5) which is 5*fac(4) which is 5*(4*fac(3)) ...
note that the fac function is not available outside of itself
var factorial = function fac(n) { return n < 2 ? 1 : n * fac(n - 1); };
console.log(fac(5));
This way of coding where a function call itself is called recursive programming it can be hard to grasp at first but can be VERY powerful in some situations
recurcive programming can easily be used when the result of func(n) depend directly on the result of func(n+x)
the cond ? then : else structure is called ternary operator, it's a way to make if in one line, can become messy very quick
as you noted in a comment : recursion can be replaced by a loop (it can be hard to do it sometime)
here's a more beginner way to write it
function factorial(n) {
if(n < 2) {
return 1
} else {
let fac = 1;
for(var i = n; i > 0; i--) {
fac = fac * i
// here fac takes the value of the precedent iteration
}
// this whole loop is the same as i*fac(i-1) in the recurcive way
return fac
}
};
console.log(factorial(5));
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have the next code in javascript:
var exponential = 2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642742746;
var numerator, denominator;
function fact(x) {
if(x==0) {
return 1;
}
return x * fact(x-1);
}
function poisson(k, landa) {
exponentialPower = Math.pow(exponential, -landa); // negative power k
landaPowerK = Math.pow(landa, k); // Landa elevated k
numerator = exponentialPower * landaPowerK;
denominator = fact(k); // factorial of k.
return (numerator / denominator);
}
I need parse to php but i don't know how...
Can somebody help me?
you should put below variables within poisson function also dont think you need to initialize $numerator and $dominator to 0;
$exponential = 2;
$numerator = 0;
$dominator = 0;
function fact($x) {
if($x==0) {
return 1;
}
return $x * fact($x-1);
}
function poisson($k, $landa)
{
$exponential = 2;
$exponentialPower = pow($exponential, -$landa);
$landaPowerK = pow($landa,$k);
$numerator = $exponentialPower * $landaPowerK;
$dominator = fact($k);
echo ($numerator / $dominator);
}
poisson(1,2);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
How was this code encrypted? A former webmaster left me code encrypted this way. I do not know how to solve.
Code:
function setCookie(a, b, c) {
var d = new Date();
d[_0x6fff[1]](d[_0x6fff[0]]() + 24 * c * 60 * 60 * 1e3);
var e = _0x6fff[2] + d[_0x6fff[3]]();
document[_0x6fff[4]] = a + _0x6fff[5] + b + _0x6fff[6] + e;
}
function getCookie(a) {
var b = a + _0x6fff[5];
var c = document[_0x6fff[4]][_0x6fff[8]](_0x6fff[7]);
for (var d = 0; d < c[_0x6fff[9]]; d++) {
var e = c[d];
while (_0x6fff[11] == e[_0x6fff[12]](0)) e = e[_0x6fff[10]](1);
if (e[_0x6fff[13]](b) != -1) return e[_0x6fff[10]](b[_0x6fff[9]], e[_0x6fff[9]]);
}
return _0x6fff[14];
}
Looks like it's a combination of minification and Hex-encoded Chinese characters:
Minification is a way of reducing the size of a javascript file by replacing long variable names with single letters (a, b, c in your example above)
_0x6fff is the HEX representation of a HAN character: cross on stepping-stones
Once code has been minified, you can't really undo it. See here