Reputation: 7601
I have the following if statement:
let panoX = 0
if (pano.x === 0) {
panoX = 0
} else if (pano.x === null) {
panoX = 200
} else {
panoX = pano.x
}
// This will be sent in a form
{
x: panoX,
y: panoY
}
It sets panoX
based on the value of pano.x
. But I also have a pano.y
, that goes through the same if statement. How can I deal with pano.y
without having to write a new if statement?
(Note: I can't do x: pano.x || 200
because 0
is falsy in JavaScript. So setting pano.x
to 0
makes x:
be 200
.
Upvotes: 0
Views: 43
Reputation: 262534
function panoFix(x){
if (x === 0) return 0;
if (x === null) return 200;
return x;
}
// This will be sent in a form
{
x: panoFix(pano.x),
y: panoFix(pano.y)
}
The first condition seems to be redundant (same as the fallback case), so it could be shortened to
function panoFix(x) { return x === null ? 200 : x }
Upvotes: 3
Reputation: 803
in JS:
var panoX = 200, panoY = 200;
if(pano !== null){
panoX = pano.x;
panoY = pano.y;
}
Upvotes: 0