How to pass a parameter with function signature - javascript

I don't have a control over the function invocation and I need to pass a few parameters with a pointer. Is it possible to do it?
var a = function(a, b){
return a + b;
}
b = a; //parameter here???
Any help will be appreciated!

You can use currying, the outer function call will return a pointer to another function that references the parameters you passed. Then you can call the inner function without passing the parameters.
var a = function(x, y) {
function(){
return x + y;
}
}
b = a(2, 3);
b();

The correct way to pass parameters to a is to replace your last line with : b = a(1, 2);.
This will give you 3 as a result, stored into the variable b.
You can see the current value of b at any time by running console.log(b); :
var a = function(a, b){
return a + b;
}
b = a(1, 2); // parameters here
console.log(b); // prints 3
But as I said, the use of b twice here is confusing. A better naming would be :
// sum is a better name
var sum = function(a, b){
return a + b;
}
result = sum(1, 2); // parameters here : 1 corresponds to a, 2 to b
console.log(result); // prints 3

Figured out myself. I am sorry if the question is not clear enough.
This is what I needed.
var a = function(a, b) {
return function(){
console.log(a + b);
}
};
b = a(3, 4);
b();

Related

Generating random integer between two inputs Javascript

I'm trying to write a function (GetPositiveInteger) that generates a random integer between variables a and b. if a > b, it's supposed to switch the values of the two so that b=>a before generating the Number.
I don't know if I'm misunderstanding the question or what because I can't figure out where I'm meant to be converting a and b to integers and my program keeps just returning NaN. here's my code maybe someone could take a look at it?
function GetRandomInteger(a, b, c) {
a = Number(a), b = Number(b);
if (a > b) {
var a = 2,
b = 1,
c = a;
a = b;
b = c;
} else {
var x = parseInt(Math.random() * b) + a;
}
return x;
}
let x;
console.log(Number(GetRandomInteger(x)));
When a > b, you're setting them to specific numbers before swapping them, instead of swapping the original values.
The code that generates the random number shouldn't be in else, because you won't run that after swapping a and b. It should just be after the if.
You don't need the c parameter. Use a temporary variable declared inside the function when swapping.
Use Math.floor() to convert a floating point number to an integer. parseInt() is for parsing strings (it will work in this case because it first converts the float to a string, but it's better to use the more specific function).
You need to call the function with two arguments. In the example below I just hard-coded them, but you can use your function that asks the user for numbers. Just use it twice to set two variables.
function GetRandomInteger(a, b) {
a = Number(a), b = Number(b);
if (a > b) {
let temp = a;
a = b;
b = temp;
}
var x = Math.floor(Math.random() * b) + a;
return x;
}
console.log(GetRandomInteger(1, 10));
console.log(GetRandomInteger(15, 3));

Interesting recursive lambda example

I just stumbled upon an interesting example of recursive lambdas and I don't really understand why it works in this manner.
rec = lambda x : 1 if x==0 else rec(x-1)*x
f = rec
rec = lambda x: x+1
print(f(10))
Same in javascript.
var rec = function(a) {
if (a == 0) return 1;
return rec(a - 1) * a;
}
var f = rec
rec = function(a) {
return a + 1;
}
console.log(f(10));
To my surprise both of these print 100 instead of 10! (as I would expect).
Why does the reassignment of rec change the behavior of the f function? When the rec variable is captured in lambda, doesn't it refer to the lambda itself?
Edit.
As most of the answers explain what is going on, let me rephrase the question because I am looking for a more in depth explanation.
So at the moment of the declaration of the function rec in the first line why doesn't the rec in the body of the function bind to itself?
For example if you take JavaScript and rewrite the first line in a seemingly "same" way as suggested in one of the answers you get:
var rec =function rec(a) {
if (a == 0) return 1;
return rec(a - 1) * a;
};
f = rec;
rec = function (a) {
return a + 1;
}
console.log(f(10));
This one prints out 10! as one would expect.
So in this case the "inner rec"(in the function body) binds to the rec of the function-name instead of looking at rec variable and the reassignment of the variable rec didn't change the behavior.
So what I am really asking is the mechanism by which those languages decide what to bind the variables in the lambdas.
I am writing an interpreter myself for a class project and I came across to the same question of when and where to bind those variables. So I wanted to understand how this works in popular languages to implement something similar.
I will address it for python because that is what i am familiar with.
First, this behaviour is only possible because python (and looks like javascript i presume) follows late-binding for their closures. further read
Late binding is when the names within a closure are looked up at runtime (unlike early binding where the names are looked up at compile time.)
What this allows is to mutate behaviours at run time, by rebinding variables that are being looked up at runtime(such as functions like rec).
The last step then is just converting the lambda functions into equivalent def syntax so the real behaviour is clearer.
The code:
rec = lambda x : 1 if x==0 else rec(x-1)*x
f = rec
rec = lambda x: x+1
print(f(10))
Can be equivalent to:
First:
def somefunc(x):
return 1 if x==0 else rec(x-1)*x
Note, python will not complain about rec not existing even on a clean session/kernel because it does not look the value up during function definition. late binding means unless this function is called, python does not care about what rec is.
Then:
rec = somefunc
f = rec
def someotherfunc(x):
return x + 1
f(10) #3628800
Now we change the rec function
rec = someotherfunc
And observe that subsequent function calls of f will use the late-bound rec, that is looked up on the function call.
f(10) #100
PS. Complete code added below:
def somefunc(x):
return 1 if x==0 else rec(x-1)*x
rec = somefunc
f = rec
def someotherfunc(x):
return x + 1
f(10) #3628800
rec = someotherfunc
f(10) #100
You could add some console.log and view, that first f is called with 10, then rec with 9 and the result is 10 * 10.
var rec = function(a) {
console.log('f', a);
if (a == 0) return 1;
return rec(a - 1) * a;
};
f = rec;
rec = function(a) {
console.log('rec', a);
return a + 1;
}
console.log(f(10));
Keeping rec.
var rec = function rec(a) {
console.log('f', a);
if (a == 0) return 1;
return rec(a - 1) * a;
};
f = rec;
rec = function(a) {
console.log('rec', a);
return a + 1;
}
console.log(f(10));
These 3 statements can sum up to a single statement
rec = lambda x : 1 if x==0 else rec(x-1)*x
f = rec
rec = lambda x: x+1
from 1 & 2
f = lambda x : 1 if x==0 else rec(x-1)*x
from above & 3
f = lambda x : 1 if x==0 else x*x
I will suggest to use variable names properly, here you don't need reassignment
why it uses second rec not the first one ?
well your function call is happening after reassignment of rec so you have the latest value in rec as
rec = function(a) {
return a + 1;
}
var f = function(a) {
if (a == 0) return 1;
return rec(a - 1) * a;
}
var rec = function(a) {
return a + 1;
}
console.log(f(10));

How to fix this triangel function in Javascript?

i am so newbie in programming, what i want is create a file which able automatically count triangel around (actually this is pythagoras problem).
i had tried to code with simple programming (the result is written below), and use function in javascript.
i use simple html file to call the javascript file, but the result is always wrong.
function fungsiKllSegitiga1(a,b) {
var c = Math.SQRT(a * a + b * b);
var kll=a+b+c;
return c.sqrt;
}
function fungsiKllSegitiga2(a,b) {
var c = (a * a + b * b)^(0.5);
var kll=a+b+c;
return c.sqrt;
}
i want variable c to count pythagoras, but i can't do it properly. any suggestion?
Try This:
function fungsiKllSegitiga1(a,b) {
var c = Math.SQRT(a * a + b * b);
return c;
}
fungsiKllSegitiga1(tmp, tmp)
tmp means temporary, change it to the numbers you want.

Arithmetic with functions in Javascript

I am trying to write a function that solves the following math problem: 3625 * 9824 + 777, using only two functions: "add" and "multiply". I am stuck here:
var multiply = function (number) {
return * 9824;
};
You need to refer to the function's argument (number) to "tell" javascript what you're multiplying:
var multiply = function (number) {
return number * 9824;
// Here^
};
Good start.
Remember that the function parameter (in this case, number) can be used as a variable inside the function itself. In your case, you want to multiply whatever number is passed in and return the result. So what you'd be looking for here is:
var multiply = function(num) {
return num * 9842;
}
:) Good luck!
first create your functions:
function add(a, b){
return a+b;
}
function multiply(a,b){
return a*b;
}
then you can call them...
let ans = 0;
ans = multiply(3625, 9824);
ans = sum(ans, 777);
console.log("3625 * 9824 + 777 = ",ans);
Keep going :) and this apply to all functional languages :D
function add(a,b)
{
return (parseInt(a)+parseInt(b));
}
function multiply(a,b)
{
return (parseInt(a)*parseInt(b));
}
var result = add(mutiply(3625,9824),777);
Try this:
function add(a, b) {
return a + b;
};
var resultAdd = add(9824, 777);
function multiply(a, b) {
return a * b;
};
var result = multiply(resultAdd, 36325);

Javascript: Use reduce() method to find the LCM in an array

I am trying to find the LCM(least common multiples) among several numbers in an array. To get a LCM between each two numbers in the array, I use the reduce() method which is not working.Please tell me what's wrong? Thanks.
function gcd(a,b){
//gcd: greatest common divisor
//use euclidean algorithm
var temp = 0;
while(a !== 0){
temp = a;
a = b % a;
b = temp;
}
return b;
}
function lcm(a,b){
//least common multiple between two numbers
return (a * b / gcd(a,b));
}
function smallestCommons(arr) {
//this function is not working, why?
arr.reduce(function(a, b){
return lcm(a, b);
});
}
smallestCommons([1,2,3,4,5]);
//------>undefined
Your smallestCommons function is missing a return. undefined is the default return value for all functions that don't have an explicit return.
function smallestCommons(arr) {
return arr.reduce(lcm);
}

Categories