Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to return toggle parameters without any condition javascript

Tags:

javascript

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
like image 671
Iftikhar Hussain Avatar asked Mar 05 '23 13:03

Iftikhar Hussain


2 Answers

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)
like image 104
Akhil Aravind Avatar answered Apr 05 '23 22:04

Akhil Aravind


return a+b-x

You can add the two numbers and subtract the incoming number.

like image 25
Dhananjai Pai Avatar answered Apr 05 '23 22:04

Dhananjai Pai