How to add parameters to a function argument without calling it - javascript

For example, addEventListener's 2-nd argument has a "e" or "evt" or "event" or "..." parameter. How can I add parameters to a function argument without calling it (without using arguments object). Is it possible?
Sorry for my bad english :)

You are looking for partial functions.

You can create functions with arguments and not provide them. For example,
function add(x, y) {
return x + y;
}
console.log(add(1, 2)); // 3
console.log(add(2)); // NaN (2 + undefined)
console.log(add(0)); // NaN (undefined + undefined)
If you don't want the function to return value like NaN, you can write this:
function add2(x, y) {
x = x || 0;
y = y || 0;
return x + y;
}
console.log(add2(1, 2)); // 3
console.log(add2(2)); // 2
console.log(add2(0)); // 0

Related

JavaScript recursive function using ternary operator

why is it necessary to add return statement before ternary operator in recursive function to return function output?
// This dose not work
function rec(n) {
n == 1 ? n : n + rec(n - 1);
}
// This works as return statement is added before ternary operator
function rec(n) {
return n == 1 ? n : n + rec(n - 1);
}
// This works
function rec(n) {
if (n == 1) return 1;
return n + rec(n - 1);
}
// If you would like to do this in one line then correct solution would be:
let rec = n => n == 1 ? n : n + rec(n - 1);
// Now you dont need to add the return keyword before
// This works as return statement is added before ternary operator
function rec(n) {
return n == 1 ? n : n + rec(n - 1);
}
// This works
function rec(n) {
if (n == 1) return 1;
return n + rec(n - 1);
}
A recursive function is function which calls itself during the execution. The ternary operator decides if the function need to call itself. So the return statement call the same function.
In the example n == 1 ? n : n + rec(n - 1); if n=1 then the function should return the value of n if not then the function will call itself with new value that is n-1.
You need a return because of
n + rec(n - 1);
where the rec(n-1) call needs to return a value to be able to calculate n + rec(n - 1), and that goes for each call to rec() until n reaches 1 when it just returns 1.
return is never default in ternary operation.
return is default in Arrow-function but it not default in normal function deceleration.
to return a output from a normal function execution it is always necessary to add return statement, but it is optional in case of Arrow-function.
function x() { 5;}
console.log(x()); // Opuput: undefined
let y = () => 5;
console.log(y()); // Output: 5
A conditional expression (often called a ternary) is simply an expression. It yields a value, but it doesn't do anything with it. In fact, unless it has side-effects, it's totally useless unless you either:
return it from a function,
assign its result to a variable, or
nest it in another expression in which you do one of these things
You may be confused by the fact that arrow functions with single-expression bodies return the result of that expression. It's still being returned by the function, even though you don't explicitly use return. And because of this simplicity, conditional expressions are often used as the body of arrow function.
But it should be no more surpising that you have to have return here than that you have to have it in
function add (x, y) {
return x + y;
}
If you took out the return there, the addition will still happen when the function is invoked, but it won't yield any value. It's the same thing in your original.

How do you add numerical values of variables rather than the numbers in JS?

VERY new, barely understand functions.
Here is an example of my issue:
function getx() {
x = 3;
}
function gety() {
y = 2;
}
getx();
gety();
document.write("The sum of x and y is " + x + y);
OUTPUT: The sum of x and y is 32
I would like to know how I can make it so x + y = 5 instead of 32. Obviously 3 + 2 isn't 32, can someone explain to me how I might output the right answer?
You're concatenating the string with x before the add operation. So, you need to wrap your Math operation with parentheses in order to avoid string concatenation.
function getx() {
x = 3;
}
function gety() {
y = 2;
}
getx();
gety();
document.write("The sum of x and y is " + (x + y));
Your functions getx() and gety() aren’t returning any values because you don’t have a return statement.
By calling the functions the way you do, you are creating two global variables: x and y, and initializing the to 3 and 2, respectively.
You should avoid using global variables in this capacity. Your global variables (or functions) can overwrite window variables (or functions). Any function, including the window object, can overwrite your global variables and functions.
Your variables should be declared with var instead.
Unless specificied, js variables are not strongly typed, And since you are using the concatenation operator before adding the variables together, it sees them as a string and this is why your concatenation is putting 3 and 2 together.
If you change your code to something like this, it should accomplish what you’re trying to achieve.
function getx() {
var x = 3;
return x;
}
function gety() {
var y = 2;
return y;
}
document.write("The sum of x and y is " + (gety() + getx()));

Javascript Calculator Function: How does it work?

I'm going through some tutorials and saw this block of code that I can't figure out. Can someone walk me through it please? I don't understand how the return ultimately executes the variable function.
var plus = function(x,y){ return x + y };
var minus = function(x,y){ return x - y };
var operations = {
'+': plus,
'-': minus
};
var calculate = function(x, y, operation){
return operations[operation](x, y);
}
calculate(38, 4, '+');
calculate(47, 3, '-');
operations is an object that has + and - as keys, so by passing one of those to it you will get
operations['+'] = plus
Now, the brackets indicate a function call which can also be made via a variable as in this case. So translated the return statement is nothing more than
return plus(x,y);
var calculate = function(x, y, operation){
return operations[operation](x, y); // operations['+'] = plus
}
Which calls above method and returns the value returned by that method.
Explanation
Look at my comments for an explanation.
var plus = function(x,y){ return x + y }; //-- var plus contains this function now.
var minus = function(x,y){ return x - y };
var operations = {
'+': plus, //-- This states that '+' contains the plus function.
'-': minus //-- This states that '-' contains the minus function.
};
var calculate = function(x, y, operation){ //-- operation makes it able to select a function from operations.
return operations[operation](x, y);
}
calculate(38, 4, '+'); //-- The '+' selects the plus function here.
calculate(47, 3, '-'); //-- The '-' selects the minus function here.
Executions will be something like this:
First argument is being passed as a key of the object and respective function is executed with arguments..
var calculate=function(x, y, operation)
{
//operations['+'](38, 4);
//operations['-'](47, 3);
return operations[operation](x, y);
};

recursion factiorial. Why do I get NaN

I have this simple factorial function. It works on recursion. the number that I get at the end however is NaN. I am expecting an output of 6, factorial of 3. Is the problem because I defined x as a global variable ? and if so how do you define a variable as local inside the function without making it change in recursion.
var x;
function factorial(num)
{
if (num > 0) {
x = num * factorial(num - 1);
}
return x;
}
console.log(factorial(3));
If num > 0, you define x. What is x when num is not > 0?
HINT: Should be 1. Isn't. :)
HINT2: Try console.log(1 * undefined)
P.S.: Yes, var x should go inside the function. No, it doesn't do anything bad in this case, but it could.
You need to initialize x with value first.
var x = 1;
Let's investigate detaily what happened:
f(3) = 3 * f(2);
f(2) = 2 * f(1);
f(1) = 1 * f(0);
For f(0), it directly goes to return x;, while your global variable var x; is not initialized, thus f(0) return undefined, and 1 * factorial(0) get NaN, propagates to the final result.
One solution is not to use global variable:
function factorial(num)
{
var x = 1;
if (num > 0) {
x = num * factorial(num - 1);
}
return x;
}
console.log(factorial(3)); // 6

What is the initial value of an argument in a function within a function?

Using the code below:
function makeAddFunction(amount) {
function add(number) {
return number + amount;
}
return add;
}
var addTwo = makeAddFunction(2);
var addFive = makeAddFunction(5);
console.log(addTwo(1) + addFive(1));
The console prints out 9. I am assuming 'number' in the add function is zero but why is the value of 'number' initially 0?
There's no "initially 0" about this.
You're first returning a function that adds 2 to a number, then making a function that adds 5 to a number.
thus, you've effectively written:
console.log((2 + 1) + (5 + 1));
and 3 + 6 is 9.
addTwo is essentially:
var addTwo = function (number) {
return number + 2;
}
add addFive is:
var addFive = function (number) {
return number + 5;
}
because you're using this as a closure.
When you call makeAddFunction, it takes the parameter you passed in for amount and then returns that inner function.
Thus when you pass in 2, it returns this:
function add(number) {
return number + 2;
}
Then you are setting that code to the addTwo variable. So when you call addTwo with a parameter of 1, it returns 1+2 (3)

Categories