Jump to content

JS: Conditional Operator


tinfanide

Recommended Posts

<html><head><title>JS: Conditional Operator (?:)</title><script>indow.onload = function(){window.input = document.getElementById("input");window.txtarea = document.getElementById("txtarea");}var x;var y;function test(){x = document.getElementById("input").value;/*y = (x == "my mum is a female.") ? "Correct! You know which gender your mum is. Good child!" : "Urgh!!! Stupid cow! Don't ya know your mum is a female???";*/if(x == ""){	txtarea.innerHTML = "";	}else if(x == "xxx"){	txtarea.innerHTML = "GOOD!";	}else {txtarea.innerHTML = "wrong!"}/*txtarea.innerHTML = y;*/}/*var x = 1;(x == 1) ? y = 1 : y = 2;document.write(y); */</script></head><body><b>Which gender is your mum?</b><br /><span>Please type your answers in the textbox below with good grammar.</span><br /><input name="name" id="input" type="text" value="" onclick="test()" /><br /><textarea id="txtarea" style="width: 300px;">This is the answer.</textarea></body></html>

I wonder if I can do something like this:

var = (condition: func) ? x : y;

Can I use a function in the condition?Cos I wanna have three conditions in the condition operator as in the if else if else statement I wrote above.

Link to comment
Share on other sites

The best way is to try it...I guess you can, but you need use return..
Thanks for reminding me of trying them out. I searched an article that taught multi possibilities of conditional operator and did something like this (tested):
x = document.getElementById("input").value;y = (x == "my mum is a female.") ? "Correct! You know which gender your mum is. Good child!" : (x == "") ? "Don't leave it blank, please!" : "Urgh!!! Stupid cow! Don't ya know your mum is a female??? Or ya needa re-take ya grammar course. Back to school!";txtarea.innerHTML = y;

The rule's:var2 = (condition 1: var 1...) ? " " : (condition 2) ? " " : " ";

Link to comment
Share on other sites

The most important reason for choosing the conditional operator or an if-else structure is readability. Embedding long strings in a conditional really spoils that. When it's a nested conditional, it's even worse. You're going to look at that in a month and wonder how it works.If you really, really want to use a conditional operator, I suggest assigning each string to a variable at the top of your function. Use the variables in your conditional.

Link to comment
Share on other sites

The most important reason for choosing the conditional operator or an if-else structure is readability. Embedding long strings in a conditional really spoils that. When it's a nested conditional, it's even worse. You're going to look at that in a month and wonder how it works.If you really, really want to use a conditional operator, I suggest assigning each string to a variable at the top of your function. Use the variables in your conditional.
That' exactly what I thought shortly after I'd posted this question.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...