function switchValue (a, b) {
return [b,a] = [a,b]
}
var a = 'computer'
var b = 'laptop'
switchValue(a, b)
console.log("a = " +a)
console.log("b = " +b)
how to change this variable, that output :
a = laptop
b = komputer
please help me
Try this
function switchValue(a, b) {
let c = a;
let a = b;
let b = c;
return [a, b];
}
You can do it like this but generally it's not advisable to use global variables
var a = 'computer'
var b = 'laptop'
function switchValue (val1, val2) {
let c = val1;
a = val2;
b = c;
}
switchValue(a, b)
console.log("a = " + a);
console.log("b = " + b);
Without global variables
function switchValue (a, b) { return [b,a] }
var a = 'computer'
var b = 'laptop'
let [A,B] = [...switchValue(a, b)]
console.log("a = " + A);
console.log("b = " + B);
Related
I am calling a function at several places within my app. This function takes several parameters whose values might change. To avoid re-typing, I want to save a reference to this function so that I can simply call this referred function everywhere. Here is the simplified code:
const func1 = (a,b,c) => a + b + c;
let a = 1, b = 2, c = 3;
const func2 = func1.bind(this, a, b, c);
func2();
//prints 6
c = 6;
func2();
//still prints 6!
How can I ge func1 to be executed with updated values of a, b and c by calling func2?
Use arrow function:
const func1 = (a,b,c) => a + b + c;
let a = 1, b = 2, c = 3;
const func2 = () => func1(a, b, c);
console.log(func2());
c = 6;
console.log(func2());
You can bind the function to an array of [a, b, c] instead, and then change the property at index 2:
const func1 = (a,b,c) => a + b + c;
const params = [1, 2, 3];
const func2 = () => func1(...params);
func2();
params[2] = 6;
func2();
If you only change c, you could consider binding the function to a and b, and then passing the changing c:
const func1 = (a,b,c) => a + b + c;
const params = [1, 2];
const func2 = (c) => func1(...params, c);
func2(3);
func2(6);
If you want to use params from scope where you declare your function, just skip those params in function signature
const f = () => a + b + c;
let a = 1, b = 2, c = 3;
console.log(f());
c = 6;
console.log(f());
Instead of this const func2 = func1.bind(this, a, b, c);
You can use this function(arrow): const func2 = () => func1(a, b, c);
function partialize(){
}
function calculation(a,b,c){
console.log(a*b/c);
return a*b/c;
}
var a = 10, b= 20, c= 5;
var partialize1 = partialize(calculation, a);
partialize1(b,c)
var partialize2 = partialize(calculation, a, b);
partialize2(c)
var partialize3 = partialize(calculation, a, b, c);
partialize3()
I need to write partialize function which give same output in all three condition.
I tried like that it work .but i used spread operator .can we do this without spread operator ?
function partialize(fn,...a) {
console.log(...a);
return function (...b) {
console.log(...b);
fn(...a,...b);
}
}
function calculation(a, b, c) {
console.log(a * b / c);
return a * b / c;
}
var a = 10, b = 20, c = 5;
var partialize1 = partialize(calculation, a);
partialize1(b, c)
var partialize2 = partialize(calculation, a, b);
partialize2(c)
var partialize3 = partialize(calculation, a, b, c);
partialize3()
can we do the same thing without spread operator ?
You can save the initial arguments that were passed and return a function that can be called with the rest of the arguments, then calling the original function with apply:
function partialize(fn) {
const initialArguments = Array.from(arguments).slice(1);
return function() {
const finalArguments = Array.from(arguments);
fn.apply(null, initialArguments.concat(finalArguments));
}
}
function calculation(a, b, c) {
console.log(a * b / c);
return a * b / c;
}
var a = 10,
b = 20,
c = 5;
var partialize1 = partialize(calculation, a);
partialize1(b, c)
var partialize2 = partialize(calculation, a, b);
partialize2(c)
var partialize3 = partialize(calculation, a, b, c);
partialize3()
If your code is currently working as is but you'd like to change it to not use the spread operator, you can use the arguments object instead.
arguments object:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
Also check out this stackoverflow question for some example code working with the arguments object if helpful. How can I convert the "arguments" object to an array in JavaScript?
user944513, to simulate an overload of methods in javascript, yo can use the arguments object, which comes by default in the functions. To explain you better, i've written a block of code. Hope this can help you:
function suma(a = 0){
let resultado = 0;
console.log(arguments.length);
for(let i = 0; i < arguments.length; i++)
{
if(typeof(arguments[i] == "number")){
resultado += arguments[i];
}
}
}
suma(1,2,3,4); // 10
suma(1,2) // 3
I found this code in a book, how do you write or define the code for mybind
var concat = function(a, b) { return a + " " + b;}
var good = mybind(concat, "good");
good("night") == "good night"
To create a new function, you can either create it yourself:
function mybind(f, a) {
return function (b) {
return f(a, b);
}
}
var concat = function(a, b) { return a + " " + b;}
var good = mybind(concat, "good");
console.log(good("night"));
or for your scenario you can use function.bind to create one for you
function mybind(f, a) {
return f.bind(null, a);
}
var concat = function(a, b) { return a + " " + b;}
var good = mybind(concat, "good");
console.log(good("night"));
Like this:
var concat = function(a, b) { return a + " " + b;}
var mybind = function (fn, arg1) {
return function (arg2) {
return fn(arg1, arg2);
};
}
var good = mybind(concat, "good");
console.log(good("night") === "good night")
The following will make your comparison return true. myBind should create a new function bound to b. that's what bind does.
var mybind = function( fn, b ) { return fn.bind(this, b); };
var add = function(a, b) {
return a + b;
}
var addOne =add.bind(null,1);
var result = addOne(4);
console.log(result);
Here the binded value of a is 1 and b is 4.
How to assign the binding value i.e)1 to the second argument of the function without using spread operator(...)
You could take a swap function with binding the final function.
var add = function (a, b) { console.log(a, b); return a + b; },
swap = function (a, b) { return this(b, a); },
addOne = swap.bind(add, 1),
result = addOne(4);
console.log(result);
With decorator, as georg suggested.
var add = function (a, b) { console.log(a, b); return a + b; },
swap = function (f) { return function (b, a) { return f.call(this, a, b) }; },
addOne = swap(add).bind(null, 1),
result = addOne(4);
console.log(result);
You could use the arguments object for reordering the parameters.
var add = function (a, b, c, d, e) {
console.log(a, b, c, d, e);
return a + b + c + d + e;
},
swap = function (f) {
return function () {
var arg = Array.apply(null, arguments);
return f.apply(this, [arg.pop()].concat(arg));
};
},
four = swap(add).bind(null, 2, 3, 4, 5),
result = four(1);
console.log(result);
You can use the following way
var add = function(x){
return function(y){
return x+y;
}
}
add(2)(3); // gives 5
var add5 = add(5);
add5(10); // gives 15
here add5() would set x = 5 for the function
This will help you what you need
var add = function(a) {
return function(b) {
return a + b;
};
}
var addOne = add(1);
var result = addOne(4);
console.log(result);
You can try this
function add (n) {
var func = function (x) {
if(typeof x==="undefined"){
x=0;
}
return add (n + x);
};
func.valueOf = func.toString = function () {
return n;
};
return func;
}
console.log(+add(1)(2));
console.log(+add(1)(2)(3));
console.log(+add(1)(2)(5)(8));
I have 2 arrays lets say:
A = [1,2,3,4,5] and B = [1,2,3,6,7]
and I'd like to perform the following 'set calculations':
C = (A ∩ B)
D = A - (A ∩ B)
E = B - (A ∩ B)
Essentially:
C = [1,2,3]
D = [4,5]
E = [6,7]
Is there a smart way to do this or am I going to have to cross check each array member with loops and ifs? I cannot use an external library (like math.js or w/e).
Thanks in advance.
filter() can at least hide the loops for you:
A = [1,2,3,4,5];
B = [1,2,3,6,7];
C = intersection(A, B);
D = arrayDiff(A, C);
E = arrayDiff(B, C);
console.log(JSON.stringify(C));
console.log(JSON.stringify(D));
console.log(JSON.stringify(E));
function intersection(a, b) {
return a.filter(
function(el) {
return b.indexOf(el) >= 0;
}
);
}
function arrayDiff(a, b) {
return a.filter(
function(el) {
return b.indexOf(el) < 0;
}
);
}
As of ES6, Javascript has an inbuilt set object, which offers neat ways to do the above operations.
var intersection = function(setA, setB){
return new Set([x for (x of setA) if (setB.has(x))]);
}
var difference = function(setA, setB){
return new Set([x for (x of setA) if (!setB.has(x))]);
}
A = new Set([1,2,3,4,5]);
B = new Set([1,2,3,6,7]);
// A ∩ B = ([1,2,3])
intersection(A, B);
// A \ B = ([4,5])
difference(A, B);
// B \ A = ([6,7])
difference(B, A);