Clean up if statement Javascript [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I do have jQuery and wouldn't mind using it but here's the code I would like to clean up:
if (parent.uglyChild.uglyChild.uglyChild.uglyChild.data == null) {
parent.uglyChild.uglyChild.uglyChild.uglyChild.data = new app.newData();
}
The only way I know how to clean it up is:
var data = parent.uglyChild.uglyChild.uglyChild.uglyChild.data;
if (data == null) { data = new app.newData()}
I have always wondered if there was a way to do something like:
parent.uglyChild.data = parent.uglyChild.data.isNull ? return : new app.newData();
I can't wait to see the shorthand tricks you guys know! :)

You could do something like
var ugly = parent.uglyChild.uglyChild.uglyChild.uglyChild;
ugly.data == null && (ugly.data = value)

you can cache the nearest object to avoid repetition, thanks to JS's leaky assignments:
if ( (x=parent.uglyChild.uglyChild.uglyChild.uglyChild).data == null) {
x.data = value;
}

If you want to use the ternary operator, you could always reassign the value.
parent.uglyChild.data = (parent.uglyChild.data == null) ? null : value;

Related

Optimising a JavaScript function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
I have created this function to check the pathname of these 3 products, but I think it looks a bit repetitive, so I'm wondering if there's a way to make it more optimised
function checkPathName() {
return (
window.location.pathname !== '/groovybaby-/241315' &&
window.location.pathname !== '/cleopatra/241162' &&
window.location.pathname !== '/cumulus/528678'
)}
checkPathName();
I'm expecting this function to return false for those pathnames
A better option would be to have a Set of these paths and then check if the pathname is in the set:
const PATHS = new Set([
'/groovybaby-/241315',
'/cleopatra/241162',
'/cumulus/528678',
]);
function checkPathName() {
return !PATHS.has(window.location.pathname)
}
You actually don't even need a separate function for this.
you can create array of URLs and then check if it includes your current path.
function checkPathName() {
let urls = ['/groovybaby-/241315', '/cleopatra/241162', '/cumulus/528678' ];
if ( urls.includes(window.location.pathname) )
return false
}

Comparison position in conditionals JavaScript [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 months ago.
Improve this question
I'm curious about, why some programers compares before the value than the variable ?
For example, if I want to create a simple if, I'll do something like this:
const foo = 'success';
if(foo === 'success') {
console.log('works fine!');
} else {
console.log('don\'t work');
}
BUT I've seen some programers that do that:
const foo = 'success';
// Note the position of the success value!
if('success' === foo) {
console.log('works fine!');
} else {
console.log('don\'t work');
}
I want to know which way is better and WHY ?
Thanks.
It's Yoda conditions. Programmers do for null safe.
I try compare strings. If my variable null and I try call method of includes() I will get exception, but if I call includes() for literal it's save from exception.
const someString = null;
// I will get exception
if (someString.includes("literal")) {
}
// Null safe
if ("literal".includes(someString)) {
}
BUT: if you matter know about something variable that it's null, then not need use yoda condition. Try handle this exception.

Variable not printing in console.log [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have large chunk of js code.
I am not able to display the output in console.log.
Can you tell me what I am missing?
I am providing my code below:
var multCoffees = false;
if (Coffees.length > 1) {
multCoffees = true;
}
if (apptTimeCell) {
apptTimeHTML = MyDay.dish(allData, multCoffees);
apptTimeCell.innerHTML = apptTimeHTML;
} else {
apptTimeCell = Util.cep("span", {
className: "appt-time"
});
patientRowTD.insertBefore(apptTimeCell, patCell);
}
dish: function (allData, multCoffees) {
if (multCoffees) {
var htmlArr = [];
htmlArr.push(allData.APPT_TIME_DISPLAY, "<br/><span class='sub-detail'>", allData.MNEMONIC, "</span>");
console.log("multiCoffee" + allData.PROVIDER_MNEMONIC);
return htmlArr.join("");
} else {
return allData.APPT_TIME_DISPLAY;
}
},
Do you mean to have the ** around your console statement? That is probably causing the error.
**console.log("dateRaj" + date);**
I know this behaviour. This happens when combining values with strings ( especially with objects). Try to output your data variable stand alone without combining it with the string. This way:
console.log("dateRaj");
console.log(date);
update
Like my commentators said the console seems to take also multiple arguments which may be the most elegant way to go.
Just guessing...
With unescapeJSON() you mean JSON.parse() ?
Show us your syntax error message?

how can i simplify or get performance in this if statement [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
this is the statement
if( validator(foo) && foo.var1 || foo.var2 || foo.var3 || foo.var4){
//do sometihng
}else{
//do anything because if foo.* doesnt exists i cant do anything
}
the statement says: validator have to valid foo, and return true, and var1 or var2 var3 or var4 have to exists.
if someone needs an explanation, just add a commentary. This is a simple question, but im trying to get performance in my code.
by the way. There is any book or tutorial that has some info about performance code in javascript.??
thank you all!
If validator(foo) is an expensive operation, you can reverse the order of the tests:
if ((foo.var1 || foo.var2 || foo.var3 || foo.var4) && validator(foo)) {
} else {
}
If validator(foo) isn't particularly expensive, there probably is no reason to spend any time at all thinking about how to improve the performance of this little piece of code. There are better uses for your time.
By the way, this code is based on your verbal description. Your code as it stands is incompatible with the verbal description and is likely buggy. In JavaScript, false && false || true evaluates to true. You probably need
if( validator(foo) && (foo.var1 || foo.var2 || foo.var3 || foo.var4)) {
Also, as Benjamin points out, foo.var1 does not test that foo.var1 exists; it checks that it exists (either in foo or in its prototype chain) and furthermore is not a "falsy" value. (For instance, if foo.var1 is 0, then it will evaluate as false.) You might be looking for foo.hasOwnProperty('var1'), etc.

javascript return, not functioning [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this code:
replaceAny('this','that',string);
if(val!="")
the replaceAny function looks like this:
function replaceAny(first,second,ele) {
var val = ele.replace(first,second);
alert(val);
return val;
}
But then after running the replaceAny function (and the alert shows the right value, the if condition tells me that the variable val is not set, why?!
You are not using the return value from the function correctly. This is something like what you want
function replaceAny(first,second,ele) {
return ele.replace(first,second);
}
var val = replaceAny('this','that',string);
if(val!=""){
//something, something darkside
}
I think you are not assigning the value returned by your function to your variable val.
Try like this
val = replaceAny('this','that',string);
if(val!="")

Categories

Resources