1

I found a exactly question like this over here but that is for C#.

I need one for javascript.

I dont want to have to enter the last value "NA" since its never going to be called if code is programmed properly. Is there such a work around so that I dont have to enter anything, by anything I mean not even " ".

(col_1 === 0)? 0 :(col_1 >= 1)? col_1-1:"NA";
  • 2
    Nope. If you want a condition without an `else` branch, use an `if` without an `else` branch. – Federico klez Culloca Mar 21 '18 at 15:52
  • where does the value get assigned? or what do you do with the value? – Nina Scholz Mar 21 '18 at 15:57
  • So if the last part cannot be called, that means that `col_1` is either zero, or it is 1 or greater. So why not just `(col_1 === 0)? 0 : col_1-1`? – TLP Mar 21 '18 at 15:58
  • Nina Scholz I believe where and what I do with it, its irrelevant to the question. –  Mar 21 '18 at 15:59
  • without, the question makse no sense. – Nina Scholz Mar 21 '18 at 15:59
  • Nina Scholz, I fail to get why would it make any different, I want to know if a tenary can have a condition without having to return the last value since its never going to be called. its makes no difference to where I assign it. (condition1 = true)? "ok":(Condition2=false)?"Ok": "DONT NEED THIS!"; makes no difference if this condtion is asign to something. there problem still remains there is there a way to not enter the last value? –  Mar 21 '18 at 16:02
  • Wouldn't it make more sense to write it like `col_1 >= 1 ? col_1 - 1 : 0`? If your column is not meant to be lower than 0 it makes sense. – Amin NAIRI Mar 21 '18 at 16:28

4 Answers4

0

You can return null or undefined, but not nothing

TLP
  • 1,262
  • 1
  • 8
  • 20
0

Maybe something like:

(col_1 <= 0) ? 0 : (col_1 - 1);
palaѕн
  • 72,112
  • 17
  • 116
  • 136
0

While unclear, what the real purpose of this question is, is suggest to use a continuing check for zero or for greater or equal to one.

This proposal does not change result if no condition is true.

if (col_1 === 0) {
    result = 0;
}
if (col_1 >= 1) {
    result = col_1 - 1;
}
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

How to not return a value from the last ternary operator?

No, you can't, the last part of a ternary operator is required. To accomplish that, you need if-else blocks.

if (col_1 === 0) {
    result = 0;
} else if (col_1 >= 1) {
    result = col_1 - 1;
}

An alternative is using the logical operator &&

Like I said, the last part is required

(col_1 === 0) ? 0 : (col_1 >= 1 && col_1 - 1);

var col_1 = 0;
var result = (col_1 === 0) ? 0 : (col_1 >= 1 && col_1 - 1);
console.log(result);

col_1 = 5;
result = (col_1 === 0) ? 0 : (col_1 >= 1 && col_1 - 1);
console.log(result);

col_1 = 3;
result = (col_1 === 0) ? 0 : (col_1 >= 1 && col_1 - 1);
console.log(result);

col_1 = -1;
result = (col_1 === 0) ? 0 : (col_1 >= 1 && col_1 - 1);
 // This case will always return false because 'col_1 < 0'
 // Here you can check for that value
console.log("You need to check this situation: ", result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Ele
  • 33,468
  • 7
  • 37
  • 75