Convert JavaScript ternary operator to if than else statement - javascript

I'm trying to convert the following ternary operator representation to an if-then else statement:
return 0 === r.length ? t ? O.toInvalid : null : o(r) ? null : O.toInvalid
Could you please help me to do this?

You could use this statement.
if (0 === r.length) {
if (t) {
return O.toInvalid;
} else {
return null;
}
} else {
if (o(r)) {
return null;
} else {
return O.toInvalid;
}
}

try this if else condition I optimize for you
if ( (!r.length && t) || (r.length && !o(r) ) ) {
return O.toInvalid;
} else {
return null;
}

Related

Breaking ternary operations to if, else in javascript

Hey guys i have a hard time in understanding multiple ternary conditions. Here is my code below
<div
className={
this.state.currentIndex == index
? "question-box bg-red-box"
: question.visited
? question.review
? "question-box review-box"
: question.selected_answer == null
? "question-box white-box"
: "question-box orange-box"
: "question-box"
}
>
How can i write this in if else (just for understanding). I know the conditions here would not be understable but just i want it in if else to get a clear understanding
Thanks !
The direct translation into if/else would be:
let temp;
if (this.state.currentIndex == index) {
temp = "question-box bg-red-box"
} else {
if (question.visited) {
if (question.review) {
temp = "question-box review-box";
} else {
if (question.selected_answer == null) {
temp = "question-box white-box"
} else {
temp = "question-box orange-box"
}
}
} else {
temp = "question-box"
}
}
// later:
<div className={temp} />
Neither version of the code is easy to understand. I would probably do something like the following instead:
let highlight = '';
if (this.state.currentIndex === index) {
highlight = "bg-red-box";
} else if (question.visited && question.review) {
highlight = "review-box";
} else if (question.visited && question.selected_answer === null) {
highlight = "white-box";
} else if (question.visited) {
highlight = "orange-box";
}
// ...
<div className={`question-box ${highlight}`} />
let currentIndex = 1;
let index = 2;
let question = {
visited: true,
review: false,
selected_answer: null
}
let output = '';
if(currentIndex == index){
output = 'question-box bg-red-box'
} else if (question.visited) {
if(question.review){
output = "question-box review-box"
} else if (question.selected_answer == null) {
output = "question-box white-box"
} else {
output = "question-box orange-box"
}
} else {
output = "question-box"
}
console.log(output)

How to avoid java script duplicate code in if statement

I have the following function with some stetemsnts inside:
isFieldVisible(node: any, field: DocumentField): boolean {
if (field.tag === 'ADDR_KOMU') {
let field = this.dfs_look(node.children, 'ADDR_APPLICANTTYPE');
return field.fieldvalue == 1;
}
if (field.tag === 'ADDR_SNAME') {
let field = this.dfs_look(node.children, 'ADDR_APPLICANTTYPE');
return field.fieldvalue == 1;
}
if (field.tag === 'ADDR_FNAME') {
let field = this.dfs_look(node.children, 'ADDR_APPLICANTTYPE');
return field.fieldvalue == 1 || field.fieldvalue == 2;
}
}
How to improve it and avoid duplicates?
I have tried to use foreach with tuple as iteration value, but I can not return boolean from foreach
You can use JavaScript OR(||) operator in if condition to avoid duplicate code.
isFieldVisible(node: any, field: DocumentField): boolean {
if (field.tag=== 'ADDR_KOMU' || field.tag=== 'ADDR_SNAME' || field.tag=== 'ADDR_FNAME' ) {
let field= this.dfs_look(node.children, 'ADDR_APPLICANTTYPE');
if(field.tag=== 'ADDR_FNAME'){
return field.fieldvalue == 1 || field.fieldvalue == 2;
}
return field.fieldvalue == 1;
}
}
Also, Check how to use the logical "OR" operator, that is || in the javascript.
A switch statement would definitely help save redundancy.
isFieldVisible(node: any, field: DocumentField): boolean {
let field;
switch(field.tag) {
case 'ADDR_KOMU':
case 'ADDR_SNAME':
field = this.dfs_look(node.children, 'ADDR_APPLICANTTYPE');
return (field.fieldvalue == 1);
case 'ADDR_FNAME':
field = this.dfs_look(node.children, 'ADDR_APPLICANTTYPE');
return (field.fieldvalue == 1 || field.fieldvalue == 2);
default:
//Do something
}
}
try this
isFieldVisible(node: any, field: DocumentField): boolean {
let field:{
tag: "ADDR_KOMU" | "ADDR_SNAME" |"ADDR_FNAME",
fieldvalue:boolean
};
switch (field.tag) {
case 'ADDR_KOMU':
case 'ADDR_SNAME':
field = this.dfs_look(node.children, 'ADDR_APPLICANTTYPE');
return (field.fieldvalue == 1);
case 'ADDR_FNAME':
field = this.dfs_look(node.children, 'ADDR_APPLICANTTYPE');
return (field.fieldvalue == 1 || field.fieldvalue == 2);
default:
return
}
}

Greater than or not equal to

My code here
if (parent4 && parent5 && parent6 && (_state[tree][parent4]) + (_state[tree][parent5]) + (_state[tree][parent6]) !== 8) {
return false;
} else {
return true;
}
It works, and has some of the elements I want, namely that if the sum of the three _states !== 8, it returns false.
I'll try to explain the logic I'm trying to achieve as simply as I can:
if par4 + par5 + par6 !>= 8
return false
if par4 + par5 !>= 8
return false
if par4 OR par5 OR par6 !>= 8
return false
else
return true
I abbreviated the code for simplicity and ease of understanding.
Swapping around return false and return true and changing it to >= doesn't work, because for all elements, even ones I don't mention here, it needs to go to return true by default.
How about this?
if (parent4 && parent5 && parent6) {
if ((_state[tree][parent4] + _state[tree][parent4] + _state[tree][parent4]) !== 8) {
return false;
} else {
return true;
}
}
Or in a one-liner:
if (parent4 && parent5 && parent6 && (_state[tree][parent4] + _state[tree][parent4] + _state[tree][parent4]) !== 8) {
return false;
} else {
return true;
}

uncaught Reference error: invalid left hand assignment

if ((value.length == 12) || (value.length == 9)) {
if ((value.length == 12)) {
if (value.substring(0, 2) = "048") { //this doesn't work in the execution
return true;
} else {
return false;
}
}
if ((value.length == 9)) {
return true;
} else {
return false;
}
} else {
return false;
}
You need == like this. you cant have a single = in an if statement
if (value.substring(0,2)=="048"){
It is because you are using the JS assignment operator. Typically var a = 123;
You want to be using === since it doesn't do type coercion. As opposed to == which does.
if (value.substring(0,2) === "048") {
// etc
}

Sort array on string and boolean in javascript

I am having difficulty getting this sort to work.
Current always has precedence over name.
I can get it to sort on the values of either current or name but not both.
My array look like this.
var arr = [{current:true, name:"A name"},
{name:"A name"}, {name:"B name"},
{current:true, name:"B name"}];
arr.sort(sort_me)
Here's the sort function.
var sort_me = function(left, right){
var value = "name";
var sort_by_val = function(){
return left[value] == right[value] ? 0 : (left[value] < right[value] ? -1 : 1);
}
if(left.current===right.current) {
sort_by_val();
}else{
if(left.current===true){
return -1;
}else{
if(right.current===true){
return 1;
}else{
sort_by_val();
}
}
}
}
You're missing a return:
if(left.current===right.current) {
return sort_by_val();
}
Otherwise your return value will be undefined if both currents are set:
var sort_me = function(left, right){
var nameorder = left.name === right.name ? 0 : (left.name < right.name ? -1 : 1);
if(
(left.current && right.current) ||
(!left.current && !right.current)
) {
return nameorder;
} else if(left.current) {
return -1;
} else {
return 1;
}
}
Try
var sort_me = function(left, right) {
var value = "name";
var sort_by_val = function() {
return left[value] == right[value] ? 0 : (left[value] < right[value]
? -1
: 1);
}
if (left.current === right.current) {
return sort_by_val(); //missing return
} else {
if (left.current === true) {
return -1;
} else if (right.current === true) {
return 1;
} else {
return sort_by_val(); //missing return
}
}
}
Demo: Fiddle

Categories