Function that takes a number and returns another number and vice versa

Adi*_*qat -2 javascript

I recently came across a question in an interview and am stumped at how to solve it. I would really appreciate any assistance.

The question asked me to create a function that received a number as a parameter (599) and returns a different number, but will also work in reverse. The issue is that I was not permitted to use any sort of conditional operator e.g. if, switch etc...

This was as far as I got:

function changeValue(v) {
  // return 599 if 395 passed and vice versa
}
Run Code Online (Sandbox Code Playgroud)

chr*_*con 6

Use an object

function changeValue(v) {
  const obj = {'395': 599, '599': 395};
  return obj[v];
}

console.log(changeValue(395));
console.log(changeValue(599));
Run Code Online (Sandbox Code Playgroud)


jdi*_*tal 5

how about this:

function changeValue(v) {
    return (599 + 395) - v;
}
Run Code Online (Sandbox Code Playgroud)

from a programming perspective, this isn't as good a solution as the one that uses an object, because an object (map/dictionary) is easier to extend to other values.