Can someone help me to solve this? actually i got an interview and they said to me to solve this?
i have a function with single parameters and in function i have 2 hard coded value like var a = 20, b = 30
the Question is how to return second value when i pass first value and the same how to return first value when i pass second parameter But without any condition like 'if', 'ternary', 'switch'
function someFn(x){
var a = 20, b = 30;
// Some logic
}
// to get - someFn(20) === 30 && someFn(30) === 20
I just found out a simple logic; just add the two variable inside function and then subsctract the parameter value. simple
function toggle(x){
var a = 20, b = 30;
return ((a+b)-x)
}
var return_value = toggle(30)
console.log(return_value)
var return_value = toggle(20)
console.log(return_value)
return a+b-x
You can add the two numbers and subtract the incoming number.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With