What is now the correct syntax for the ternary expression?
Hi.
JSLint now does not like this code (that it used to find OK), saying
"Wrap a ternary expression in parens, with a line break after
the left paren."
=================
orb = (orb0Btn.checked)
? 0
: (orb5Btn.checked)
? 5
: (orb10Btn.checked)
? 10
: (orb15Btn.checked)
? 15
: 20;
=================
What is now the correct syntax for the ternary expression?
emf <emfril@gmail.com> writes:
What is now the correct syntax for the ternary expression?
IIRC, JSLint does not check syntax but its authors idea of style.
The syntax for the conditional expression is
ConditionalExpression[In, Yield, Await] :
ShortCircuitExpression[?In, ?Yield, ?Await]
ShortCircuitExpression[?In, ?Yield, ?Await] ? AssignmentExpression[+In, ?Yield, ?Await] :
AssignmentExpression[?In, ?Yield, ?Await]
, so, you can just write, for example, "0?1:1?2:3"
(which would evaluate to 2).
orb = ((orb0Btn.checked)
? 0
: (orb5Btn.checked)
? 5
: (orb10Btn.checked)
? 10
: (orb15Btn.checked)
? 15
: 20);
On Thursday, July 14, 2022 at 3:44:28 PM UTC-5, emf wrote:
Hi.
JSLint now does not like this code (that it used to find OK), saying
"Wrap a ternary expression in parens, with a line break after
the left paren."
=================
orb = (orb0Btn.checked)
? 0
: (orb5Btn.checked)
? 5
: (orb10Btn.checked)
? 10
: (orb15Btn.checked)
? 15
: 20;
=================
What is now the correct syntax for the ternary expression?
It's just a jslint setting. You can turn that off I'm pretty sure.
I think it wants:
(
p ? truePart : (
p ? truePart : falsePart
)
)
I think this is because nested ternaries can be a little hard to read if you don't format them properly.
I prefer ESLint
orb =
orb0Btn.checked? 0:
orb5Btn.checked? 5:
orb10Btn.checked? 10:
orb15Btn.checked? 15:
20;
[...]
The only now warning is:
1. Bad assignment to 'panel'. 89.5
panel = document.getElementById("panel");
however the program (see sig below in (nataltransits2.js) still
does not work...
Eustace
ram@zedat.fu-berlin.de (Stefan Ram) writes:
orb =
orb0Btn.checked? 0:
orb5Btn.checked? 5:
orb10Btn.checked? 10:
orb15Btn.checked? 15:
20;
Or,
orb =
orb0Btn. checked? 0:
orb5Btn. checked? 5:
orb10Btn.checked? 10:
orb15Btn.checked? 15:
20;
. Now, imagine that someone does not know all details of the
ternary operator well, but has a basic idea of it. In which
way could he be mislead by this?
On Friday, July 15, 2022 at 1:24:37 PM UTC-5, emf wrote:
[...]
The only now warning is:
1. Bad assignment to 'panel'. 89.5
panel = document.getElementById("panel");
however the program (see sig below in (nataltransits2.js) still
does not work...
Eustace
ESLint points out a number of issues remaining:
<https://emf.neocities.org/nt/nataltransits2.js>
11:9 'ephem' is not defined. (no-undef)
15:16 'ephem' is not defined. (no-undef)
19:9 'ephem' is not defined. (no-undef)
20:9 'ephem' is not defined. (no-undef)
29:9 'reader' is not defined. (no-undef)
30:16 'reader' is not defined. (no-undef)
38:5 'reader' is not defined. (no-undef)
39:5 'reader' is not defined. (no-undef)
40:5 'reader' is not defined. (no-undef)
49:16 'getBDay' is not defined. (no-undef)
81:9 'findTransits' is not defined. (no-undef)
83:9 'panel' is not defined. (no-undef)
89:5 'panel' is not defined. (no-undef)
91:22 'e' is defined but never used. (no-unused-vars)
You have to declare a variable before assigning to it.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 251 |
Nodes: | 16 (3 / 13) |
Uptime: | 05:39:29 |
Calls: | 5,564 |
Calls today: | 3 |
Files: | 11,680 |
Messages: | 5,124,195 |
Posted today: | 1 |