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 9 years ago.
Improve this question
I'm trying to learn to shorthand my javascript but stuck with the following one.
if (windowwidth >= 960){
widthofwindow = 1;
yooucandoit()
} else {
widthofwindow = 0;
$('#topbar').remove();
}
That code looks fine. It's easily unterstandable and has very little duplication of code.
You can make it really short, if the yooucandoit function doesn't depend on the widthofwindow variable, but this uses side effects in the conditional operators, so it has a pretty bad code smell...
widthofwindow = windowwidth >= 960 ? yooucandoit(), 1 : $('#topbar').remove(), 0;
If all you wanted was to set the value of widthofwindow, you could do it with a ternary operator like so:
widthofwindow = (windowwidth >= 960) ? 1 : 0;
However, for the other statements in your code you still need to use an if statement.
you can use the ternary operator and then do the if on the widthofwidth
widthofwidth = (windowwidth>=960) ? 1 : 0;
if (widthofwidth) youcandoit() else $('#topbar').remove();
(widthofwindow = +(windowwidth >= 960)) ? yooucandoit() : $('#topbar').remove();
Using Ternary operator
condition ? ifTrue : ifFalse
and casting to number with +. Combining this with the fact
(foo = bar) === bar
Related
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 1 year ago.
Improve this question
it is hard to find a solution to this question, so I have a short way to do that. To find the count of numbers that's divisible to 3 and 4 between 1 and 100 we have a formula in Math: |b - a|: LCM (Lowest Common Multiple) + 1. I have all the stuff that is needed to create this program but I had a few issues with my code so I couldn't finish it. Can someone help me?
var totalCount3=0;
var totalCount4=0;
for(var i=1;i<=100;i++){
if(i % 3 == 0){
totalCount3 += 1;
}
if(i%4 == 0){
totalCount4 += 1;
}
}
console.log(totalCount3);
console.log(totalCount4);
The range of values you are checking is very limited (1-100) so you can use a bruteforce approach. You can simply scanning all values from to 100 and then check if each value is divisible using the module operator. I suppose you use javascript:
var counter = 0;
for(var i = 1; i<=100; i++){
if((i%3==0)||(i%4==0)){
counter++
}
}
console.log(counter);
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 years ago.
Improve this question
I have a colleague who uses ternary operators this way (In javascript):
var genderLabel = '';
isMale? genderLabel = 'Man' : genderLabel = 'Woman';
In C#, I'd just do that .
var genderLabel = isMale? "Man" : "Woman";
My colleague says it's a javascript coding convention... is that true? I'm no javascript expert and I dislike that language... When I review code, I focus on my left hand-side to follow a variable initialization or assignment, that kind of style forces me to read the whole line.
I'm also maintaining a Java code of an ex-employee, he uses ternary operators the same way. Is that an anti-pattern? I think it should be disallowed by the compiler the same way it's disallowed in a if statement :
if(x = 2)
{
...
}
This won't compile in C#.
My colleague says it's a javascript coding convention... is that true?
No.
Is that an anti-pattern?
Usually a line by itself is a statement, however this
isMale ? genderLabel = 'Man' : genderLabel = 'Woman';
is an expression, with a side effect of setting the value of genderLabel. Is that a good practice? I don't know, however if you think that is a good practice, then you will also have to allow this:
var a = 1, b = 2;
b = [a][a = b, 0]; # swap a and b
Your colleague might as well do:
if(isMale) genderLabel = 'Man';
else genderLabel = 'Woman';
which is much clearer.
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 8 years ago.
Improve this question
I don't know what is better to use:
var s = "2";
var i = 2;
if(s.toString() === i.toString()){
//do something
}
//OR
if(s == i){
//do something
}
Thanks for the help
You are actually comparing two separate things, in first, you are casting both the variable values to a string and comparing, and the other comparison is lose one i.e you are not actually checking the data types of those variables. so it will return true if you compare string with int with a same value.
According to me, what you should be using is === which will not only compare the values but their data types as well because the ones you are using are both considered lose.
If you do not consider at all about data type then using == will suffice. You don't have to cast the values to a string.
In your first example if for any reason you get a 2 with a space, it will evaluate false (even with ==):
var s = " 2"; // 2 with a sneaky space
var i = 2;
if(s.toString() === i.toString()){ // will be false
//do something
}
Personally I prefer using ===, but I would change the values to integers, instead of to strings.
var s = " 2"; // 2 with a sneaky space again
var i = 2;
if(Number(s) === Number(i)){ // will be true
//do something
}
You don't need the second Number() but, I don't know, you may get data that is also a string.
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
How would I get my XCount to go to 100.
xCount==100;
var x= 'insert code here';
for (xCount = 1; xCount < 100 && x == x.constructor; xCount++)
x = x.constructor;
x has got a method constructor(), but not a property constructor. By setting x=x.constructor in your loop you avoid the exit condition of the loop. But the loop has already "finished before it was started" (i.e. it never ran) because the condition was not met when entering the loop!
If you look at x.construcor with console.log(x.constructor) you get something like
function String() {
[native code]
}
If you want the loop to run you must set
x=x.constructor
before the loop!
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 9 years ago.
Improve this question
This is just an example but there are a bunch of similar scenarios sprinkled throughout the code so it would be nice to choose the best solution and stick with it. The first is probably quickier but the second takes up less space*
*less space when reading/fixing code and less space when downloading.
if (imageCountCurrent <= 0){
$('#next_arrow').addClass('disabled');
}
else{
$('#next_arrow').removeClass('disabled');
}
if (imageCountCurrent >= imageCount - 5){
$('#prev_arrow').addClass('disabled');
}
else{
$('#prev_arrow').removeClass('disabled');
}
$('#.arrow').removeClass('disabled');
if (imageCountCurrent <= 0){
$('#next_arrow').addClass('disabled');
}
if (imageCountCurrent >= imageCount - 5){
$('#prev_arrow').addClass('disabled');
}
You may want .toggleClass instead, which eliminates the need for verbose if/else and addClass/removeClass constructs:
$('#next_arrow').toggleClass('disabled', imageCountCurrent <= 0);
$('#prev_arrow').toggleClass('disabled', imageCountCurrent >= imageCount - 5);