Mathematics functions by string, JS [closed] - javascript

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 7 years ago.
Improve this question
I'm looking for a function that return me a mathematic function following those examples:
The_Function_I_Need("x^2") = function(x) { return math.pow(x, 2) }
The_Function_I_Need("x*2+5") = function(x) { return 2*x+5 }
The_Function_I_Need("x+y") = function(x, y) { return x+y }
And more..

var The_Function_I_Need = {
"x^2": function(x) {
return Math.pow(x, 2);
},
"x*2+5": function(x) {
return 2 * x + 5;
},
"x+y": function(x, y) {
return x + y;
}
}
console.log(The_Function_I_Need["x^2"](4))
console.log(The_Function_I_Need["x*2+5"](4))
console.log(The_Function_I_Need["x+y"](2, 4))

The Javascript eval() function can evaluate a string as an expression.
To follow your example, you could just use eval("x^2").
Reference: http://www.w3schools.com/jsref/jsref_eval.asp

Related

How to refactor this code with if statement [closed]

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 1 year ago.
Improve this question
I have code with if statement (below). Is it possible to refactor this code? because in this method is ugly if statement :(
getOverallAmount(estimate) {
const {installments} = estimate;
const {vipValue} = estimate;
if (installments !== 1) {
const oneInstallment = vipValue / installments;
const allWithOneInst = vipValue - (oneInstallment * installments) + 1;
return oneInstallment + allWithOneInst;
}
return vipValue;
}
Using a ternary operator instead of the if statement:
getOverallAmount(estimate) {
const {installments} = estimate;
const {vipValue} = estimate;
const oneInstallment = vipValue / installments;
const allWithOneInst = vipValue - (oneInstallment * installments) + 1;
return (installments !== 1) ? oneInstallment + allWithOneInst : vipValue;
}

Increase values of elements in an array [closed]

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 3 years ago.
Improve this question
Say I have an array like so...
let myArr = [0,0,2,0,0];
I want to create sort of a ripple effect such that the result of the array is [0,1,2,1,0]
This would give you the result you expect:
let myArr = [0, 0, 2, 0, 0];
createRippleArray = (myArr) => {
if (myArr.length % 2 === 0) {
console.error("createRippleArray: Array length needs to be odd number>1");
return [];
}
let midIndex = ~~(myArr.length / 2);
let mid = myArr[midIndex];
return myArr.map((e, i) => {
let res;
if (i < midIndex) {
return ~~(mid / Math.abs(midIndex - i + 1));
} else if (i === midIndex) {
return mid;
} else if (i > midIndex) {
return ~~(mid / Math.abs(midIndex - i - 1));
}
});
}
console.log(createRippleArray(myArr));
Hope this helps!

Parse poisson function in javascript to php [closed]

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);

Repeat function takin previous result of this function [closed]

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)

Function in function Javascript [closed]

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 9 years ago.
Improve this question
Task: to write a function foo.
function arg1(a,b) {
return a + b;
}
var x = foo(arg1, arg2);
x(arg3); // return arg2 + arg3
var y = foo(arg1, arg2, arg3);
y(); // return arg2 + arg3
I can't understand how at first var x, y - numbers ( because arg1() return number ), and then x, y - functions. How is it possible?
What you want is higher order function curry, a function that will return another function
function add(a,b) {
return a + b;
}
function curry(fun) {
var args = [].slice.call(arguments);
args.shift();
return function() {
return fun.apply(null, args.concat([].slice(arguments)));
};
}
x = curry(add, 10, 20);
x();
if you want to add more then two arguments you need to rewrite add function, you can iterate or use another higher order function reduce.
function add() {
[].slice.call(null, arguments).reduce(function(a, b) { return a+b; });
}
Or you can use #PaulS. solution using reduce in your foo function to call it to more then one argument:
function foo(fun) {
var args = [].slice.call(arguments);
args.shift();
return function() {
return args.concat([].slice.call(arguments)).reduce(fun);
};
};
var x = foo(add, 10, 20, 30);
x(30);

Categories

Resources