-2

I have this code F.onmousewheel = function (e) {zoomFactor = e.wheelDelta > 0 ? 10 : 11;}

Here it is, the smaller version. But I must (and Im learning) use the bigger version, that looks like this:

F.onmousewheel = function (e) {

};

My qestion is: how looks {zoomFactor = e.wheelDelta > 0 ? 10 : 11;} in the bigger version?

isherwood
  • 58,414
  • 16
  • 114
  • 157

3 Answers3

1

zoomFactor = e.wheelDelta > 0 ? 10 : 11; is an expression, so you can simply use that line:

F.onmousewheel = function (e) {
  zoomFactor = e.wheelDelta > 0 ? 10 : 11;
};

However, if you wish to break apart the ternary expression (the ? and : parts), you could do the following:

F.onmousewheel = function (e) {
  if (e.wheelData > 0) {
    zoomFactor = 10;
  } else {
    zoomFactor = 11;
  }
};

The ternary expression is essentially saying if e.wheelDelta is greater than 0, return 10, otherwise return 11. The zoomFactor variable then gets assigned that returned value. To break this out into an if/else statement, you just have to understand the ternary syntax.

The ? denotes the end of the boolean expression, which is e.wheelDelta > 0. So we use that as our boolean expression for the if statement.

The : separates the two options, the first being the value if true, the second if false. So those become our if and else assignments to zoomFactor.

Michal
  • 2,532
  • 1
  • 19
  • 27
1

You can expand the ternary operator:

F.onmousewheel = function (e) {zoomFactor = e.wheelDelta > 0 ? 10 : 11;}

Into an if else block:

F.onmousewheel = function (e) {
    if(e.wheelDelta > 0) {
        zoomFactor = 10;
    }
    else {
        zoomFactor = 11;
    }
};
Community
  • 1
  • 1
1

Your question is a little unclear, but I am guessing this is what you are looking for:

F.onmousewheel = function(e) {
    if (e.wheelDelta > 0) {
        zoomFactor = 10;
    } else {
        zoomFactor = 11;
    }
};
dhouty
  • 1,969
  • 12
  • 21