'Extract this nested ternary operation into an independent statement
I am getting the above error with the example code below, I am quite new to nested ternary operations so your help would be appreciated. Example code below:
Car4 = {
data: {
CoverBasis: item.coverBasis() || "Client",
IncludeInPackage: item.canBeIncludedInPackage() ?
(item.includeInPackage() && totalPolicies > 1 ? "Yes" : "No") : "No"
}
};
Solution 1:[1]
I think the error tells you to move the nester ternary operator totalPolicies > 1 ? "Yes" : "No" in the separated variable, like this:
const areTotalPoliciesBiggerOne = totalPolicies > 1 ? "Yes" : "No";
Car4 = {
data: {
CoverBasis: item.coverBasis() || "Client",
IncludeInPackage: item.canBeIncludedInPackage() ?
(item.includeInPackage() && areTotalPoliciesBiggerOne) : "No"
}
};
Solution 2:[2]
You forgot to add bracket to nested condition:
Car4 = {
data: {
CoverBasis: item.coverBasis() || "Client",
IncludeInPackage: item.canBeIncludedInPackage() ?
((item.includeInPackage() && totalPolicies > 1) ? "Yes" : "No") : "No"
}
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Max |
| Solution 2 | Giovanni Esposito |
