I have pushed some data into an array:
if(target === 'create'){
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' || name === 'form[plainPassword]'){
errors.push(name);
}
} else {
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' ){
errors.push(name);
}
}
It actually works fine. But it seems to me that it is really too much repeating code, but I still cannot find a way to reduce the code, or make a simpler better solution.
I would do it in two methods
function CheckErrors(target,name){
switch(target){
case 'create':
SaveError(name,true);
break;
default:
SaveError(name);
break;
}
}
function SaveError(name,checkPassword){
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' ||(checkPassword && name === 'form[plainPassword]')){
errors.push(name);
}
}
You could use arrays to simplify your if statement:
if((target === 'create' && name === 'form[plainPassword]') || ['form[username]', 'form[name]', 'form[slug]'].includes(name)){
errors.push(name);
}
If I rigth understand you, you want to simplify your statements. From my perspective it could be like this:
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]'
|| (name === 'form[plainPassword]' && target === 'create')){
errors.push(name);
}
Since:
name === 'form[username]' || name === 'form[name]' || name === 'form[slug]'
is repeated two times, it doesn't matter if target === 'create' is true or false for this statements.
In fact just add (name === 'form[plainPassword]' && target === 'create') to if statement and that's all
Related
In event.js, to judge the same handler, here is code:
return handler
&& (!event.e || handler.e == event.e)
&& (!event.ns || matcher.test(handler.ns))
&& (!fn || zid(handler.fn) === zid(fn))
&& (!selector || handler.sel == selector)
Why use (!fn || zid(handler.fn) === zid(fn)) to (!fn || handler.fn === fn)
Here is the source code of zid
var _zid = 1
function zid(element) {
return element._zid || (element._zid = _zid++)
}
if I have to judge two functions, a === b is enough
why to use zid(a) === zid(b)? Maybe some trap?
I don't know why?
Here is the source code of zepto.js event.js: https://github.com/madrobby/zepto/blob/master/src/event.js
I have a condition as below to check the variables Qmart and linearstatus.
Though they have the values as required, my if statement is always going to the else condition. Is there some mistake I'm making for checking these values?
var Qmart = "A2";
var linearstatus = "linear"
if (Qmart === ("E2" || "A2" || "B2" || "D2") && linearstatus == "linear") {
} else {
alert("it is an else condition");
}
Change the if condition to the following
if ((Qmart === "E2" || Qmart === "A2" || Qmart === "B2" || Qmart === "D2") && (linearstatus == "linear")) {
} else {
alert("it is an else condition");
}
Hope it helps
If you want to check for multiple values you cannot use Qmart === ("E2" || "A2" || "B2" || "D2"), you need to check each condition separatelly:
if ((Qmart === "E2" || Qmart === "A2" || Qmart === "B2" || Qmart === "D2") && linearstatus == "linear") {
} else {
alert("it is an else condition");
}
You might ask why your code did not throw any runtime error. The issue is that code
"E2" || "A2" || "B2" || "D2"
is a valid JavaScript code, and it returns first truthy value. This feature is called short-circuit evaluation and is used often in JavaScript. In your case above statement returns first truthy value, which is always "E2". This means that, your code is identical to
if (Qmart === "E2" && linearstatus == "linear")
and when Qmart is A2, then the condition evaluates to false and else statement is executed.
You could do something like this.
var QMart = "E2",
linearstatus = "linear";
var conditions = ["E2", "A2", "B2", "D2"];
if (conditions.indexOf(QMart) !== -1 && (linearstatus == "linear")) {
console.log("In truthy condition");
} else {
console.log("it is an else condition");
}
I have this:
if (THIS.target.hasClass('icon-false-shape')) {
$(this).addClass('white-font');
if (THIS.answer === false) {
console.log('EVERYTHING COMES GREEN');
$(this).addClass('background-green');
} else {
$(this).addClass('background-red');
}
}
if (THIS.target.hasClass('icon-true-shape')) {
$(this).addClass('white-font');
if (THIS.answer === true) {
console.log('EVERYTHING COMES GREEN');
$(this).addClass('background-green');
} else {
$(this).addClass('background-red');
}
}
which I am trying to turn into this:
$(this).addClass('background-' + ((THIS.target.hasClass('icon-false-shape') && THIS.answer === false) ? 'green' : 'red'))
.addClass('white-font');
$(this).addClass('background-' + ((THIS.target.hasClass('icon-true-shape') && THIS.answer === true) ? 'green' : 'red'))
.addClass('white-font');
But my logic is failing.
Any suggestions?
I would use $.toggleClass, that handles condition internally - see:
$(this).addClass('white-font');
$(this).toggleClass('background-green', THIS.answer === true);
$(this).toggleClass('background-red', THIS.answer === true);
Or catching both cases, moving the condition in a separate variable:
var shouldBeGreen = THIS.target.hasClass('icon-false-shape') && THIS.answer === false;
var shouldBeRed = THIS.target.hasClass('icon-true-shape') && THIS.answer === true;
$(this).toggleClass('background-green', shouldBeGreen);
$(this).toggleClass('background-red', shouldBeRed);
First we focus on the condition to be either red or green (in this case I use the criteria to be "red"):
(THIS.target.hasClass('icon-false-shape') && THIS.answer !== false)
or, (THIS.target.hasClass('icon-true-shape') && THIS.answer === false)
if none of the criteria is met, it should be "green".
$(this).addClass('white-font background-' + (((THIS.target.hasClass('icon-false-shape') && THIS.answer !== false) || (THIS.target.hasClass('icon-true-shape') && THIS.answer === false)) ? 'red' : 'green'));
Also note that you can use a space to add multiple classes $(this).addClass("white-font " + ...)" instead of this $(this).addClass(...).addClass("white-font") .
I have an If statement that using || with an && operator e.g if((a || b) && c) however it is only works with the first condition i.e a but not with second i.e b even though running the debugger I can see that the condition is met and it goes to the correct line of code. Is there a better way to get this to work on both conditions?
code I have now:
function _getCatFormGUID(catName) {
debugger;
var dept = Browser.getValue(getElement("126D81CA203C21CF014C8A3550227892FE4B4A6A"));
if((catName == '1' && dept == "Entwicklung") || (catName == '7' && dept == "Entwicklung")){
return "A270AE7F957A74EF0842403EEA0032017567F3E8";
}
if((catName == '1' && dept != "Entwicklung") || (catName == '7' && dept != "Entwicklung")) {
return "8EDD0768A7CDF8FD8AE90DB473F41EF0B33FA14F";
}
return "";}
I have tried the following also:
if((catName == '1' || catName == '7') && dept == "Entwicklung"){
return "A270AE7F957A74EF0842403EEA0032017567F3E8";
}
and
if(catName == '1' && dept == "Entwicklung"){
return "A270AE7F957A74EF0842403EEA0032017567F3E8";
}
if(catName == '7' && dept == "Entwicklung"){
return "A270AE7F957A74EF0842403EEA0032017567F3E8";
}
It only returns for catName =='1'.
If I understood your problem correctly, I will write your first bit of code as bellow
function _getCatFormGUID(catName) {
var dept = Browser.getValue(getElement("126D81CA203C21CF014C8A3550227892FE4B4A6A"));
if (catName == '1' || catName == '7') {
if(dept == 'Entwicklung'){
return "A270AE7F957A74EF0842403EEA0032017567F3E8";
}
else{
return "8EDD0768A7CDF8FD8AE90DB473F41EF0B33FA14F";
}
}
else{
return "";
}
}
The problem is that you have to understand how those 2 logical operators do the comparison: because your first condition catName == '1' is true, it will never go to second conditions from the first parenthesis nor in the second paranthesis.Given your example, you might rewrite your logical condition from:
if((catName == '1' && dept == "Entwicklung") || (catName == '7' && dept == "Entwicklung")){
return "A270AE7F957A74EF0842403EEA0032017567F3E8";
}
to
if(dept == "Entwicklung" && catName == '1' || catName == '7'){ return something; }
}
How to right this syntax correctly:
if (tipoTropaPrioritaria[m] || troopsCount[m] || availableTroops[m]) == ("null" || "undefined") {
...
}
(to check if any of the first 3 variables is null or undefined)
You could define a small helper function that does the check and then use it on all the values:
function notset(v) {
return (v === undefined) || (v === null);
}
if (notset(tipoTropaPrioritaria[m]) || notset(troopsCount[m]) ||
notset(availableTroops[m])) {
...
}
Use:
if (tipoTropaPrioritaria[m] == null || troopsCount[m] == null || availableTroops[m] == null ||
tipoTropaPrioritaria[m] == undefined || troopsCount[m] == undefined || availableTroops[m] == undefined) {
// ...
}
EDIT: It's probably better to use the === (threequals) operator in this case.
if (tipoTropaPrioritaria[m] === null || troopsCount[m] === null || availableTroops[m] === null ||
tipoTropaPrioritaria[m] === undefined || troopsCount[m] === undefined || availableTroops[m] === undefined) {
// ...
}
or:
if (null in {tipoTropaPrioritaria[m]:0, troopsCount[m]:0, availableTroops[m]:0} || undefined in {tipoTropaPrioritaria[m]:0, troopsCount[m]:0, availableTroops[m]:0}) {
The way to do is:
if ((tipoTropaPrioritaria[m] == null || tipoTropaPrioritaria[m] == undefined)
|| (troopsCount[m] == null || troopsCount[m] == undefined) ||
(availableTroops[m] == null || availableTroops[m] == undefined)) {
...
}
If you do this a lot you can create a helper function
function isNullOrUndef(args) {
for (var i =0; i < arguments.length; i++) {
if (arguments[i] == null || typeof arguments[i] === "undefined") {
return true
}
}
return false
}
if (isNullOrUndef(tipoTropaPrioritaria[m], troopsCount[m], availableTroops[m]))
...
This is the best way to do what you want
if (!tipoTropaPrioritaria[m] || !troopsCount[m] || !availableTroops[m]) {
...
}
The ! operator coerces the result into a boolean that can be tested for (null and undefined becomes false), and with the ! in front false is negated into true.
The other way to do it is to test each expression against null and undefined.
function isNullOrUndefined(val) {
return (val === null || typeof val == "undefined");
}
if (isNullOrUndefined(a) || isNullOrUndefined(b) ... ) {
And so you know it, the correct way to test for undefined is
if (typeof foo === "undefined") {...
if (tipoTropaPrioritaria[m] && troopsCount[m] && availableTroops[m]) { }
else { /* At least ones is undefined/null OR FALSE */ }
if (tipoTropaPrioritaria[m] == null || troopsCount[m] == null || availableTroops[m] == null)
{ /* One is null. */ }
if you want to check if they are null or undefined you can write
if (!tipoTropaPrioritaria[m] || !troopsCount[m] || !availableTroops[m])
both null and undefined are falsely values. Then this will only be true if none of this are null or undefined.
Please consider that there are other falsely values as well:
0
empty string
NaN