Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
My checkforZeroQuantity function is not getting called if I use it with if. Following is the sample code
function myButtonClicked() {
if (checkforZeroQuantity()) {
alert("checked");
}
}
function checkforZeroQuantity() {
var x = 1;
if (x == 0) {
return false;
} else {
retrun true;
}
}
This is because you have spelling mistake near as shown below
else {
retrun true; // it should be return
}
correct the spelling and try again.
look here:
function checkforZeroQuantity() {
var x = 1;
if (x == 0) {
return false;
} else {
return true;
}
}
There spelling mistake in code, please change 'retrun' to 'return'.
else
return true;
Firstly, there is a typo in the else block.:)
Secondly, it's a good practice to use === instead of == in Javascript
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
Is there a one line way of doing a if statement that only fires once with a boolean?
var boolean;
if (!boolean) {
function doSomething();
boolean = true;
}
Something in the lines of this.
You could take the logical OR assignment ||= with a comma operator.
boolean ||= (doSomething(), true);
It does not make much sense to do it as one line since the code is clear the way you have it written (minus your syntax error), but it can be done
function test(myBool) {
function doSomething () { console.log('run'); }
myBool = myBool || doSomething() || true;
console.log(myBool);
}
test(false);
test(true);
or if doSomething returns a true boolean or truthy value
function test(myBool) {
function doSomething () { console.log('run'); return true; }
myBool = myBool || doSomething();
console.log(myBool);
}
test(false);
test(true);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
the following code returns nothing. Is there something I am missing here? Shouldn't this say 'hi' twice...
Thanks in advance
var done = 1;
var id;
id = setInterval(function() {
if(done > 3) {
console.log('hi');
done++;
} else {
clearInterval(id);
}
}, 500);
The if statement in the interval, directly terminated the interval because 1 > 3 == false
var done = 1;
var id;
id = setInterval(function() {
if(done < 3) {
console.log('hi');
done++;
} else {
clearInterval(id);
}
}, 500);
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 7 years ago.
Improve this question
i have a problem for checking javascript function containing this keyword or not.
so, this is my code :
function check(el) {
if (el === this) {
return el;
}
else {
alert("not contain this keyword");
}
}
check(this);
it's always showing alert()
i try el == this,
i try el = this
both are still not working.
is it possible to use typeof checking?
what's wrong with my code?
If you're trying to match the string(wrap the text inside single/double quote). Use the below code.
function check(el) {
if (el === 'this') {
alert("contain!");
return el;
} else {
alert("not contain this keyword");
}
}
check('this');
If you're trying to match the text "this", wrap single or double quotes around this. The below code alerts if passed string contains "this"
function check(e1) {
if (e1.indexOf("this") > -1) {
alert("contain!");
return e1;
} else {
alert("not contain this keyword");
}
}
check("this");
The below code checks if passed word is just "this"
function check(el) {
if (el === "this") {
alert("contain!");
return el;
} else {
alert("not contain this keyword");
}
}
check("this");
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I've got this function but I don't know if "switch" would be better?
(function ($) {
var doc = $.urlParam('doc');
if (doc) {
if (doc = 'new') {
alert(doc);
}
if (doc = 'new2') {
alert(doc);
}
if (doc = 'new3') {
alert(doc);
}
}
})(jQuery);
The alert should only show up if the parameter in the URL is right, like in the IF statement.
The complete code can be found here: https://jsfiddle.net/yc5f9ct7/4/
You have to use == instead of = to check equality in if statements:
(function ($) {
var doc = $.urlParam('doc');
if (doc) {
if (doc == 'new') {
alert(doc);
}
if (doc == 'new2') {
alert(doc);
}
if (doc == 'new3') {
alert(doc);
}
}
})(jQuery);
As mentioned, double equals required, but you could also shorten this to:
(function ($) {
var doc = $.urlParam('doc');
if (doc) {
if (doc == 'new' || doc == 'new2' || doc == 'new3') {
alert(doc);
}
}
})(jQuery);
As everyone pointed out, you are using assignment = instead of comparison == or exact comparison ===.
That aside, if you are testing one variable for multiple values, and intend to have different code on each, then a switch is more logical:
var doc = $.urlParam('doc');
switch (doc){
case 'new':
alert(doc);
break;
case 'new2':
alert(doc);
break;
case 'new3':
alert(doc);
break;
}
Also note: it looks like your code wants to wrap that last part in a DOM ready handler too, but you are wrapping it in a IIFE instead. Change the wrapper to jQuery(function($){ YOUR CODE HERE });
e.g.
jQuery(function($){
var doc = $.urlParam('doc');
switch (doc){
case 'new':
alert(doc);
break;
case 'new2':
alert(doc);
break;
case 'new3':
alert(doc);
break;
}
});
This handy shortcut for DOM ready, provides a locally scoped $.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For some reason, the evaluator if isEven(n) is giving an error... can you spot an issue with the code?
function isEven(value) {
//value = Number(value);
if (value%2 == 0)
return true;
else
return false;
}
function testCondition {
if isEven(n) {
}
else {
}
}
There are two syntax errors in your code:
function isEven(value) {
//value = Number(value);
if (value%2 == 0) // no parens here is allowed so no syntax error
return true;
else
return false;
}
function testCondition() {
// ^^ missing parens here
if (isEven(n)) {
// ^ ------- ^ -- and here
}
else {
}
}
if isEven(n) {
You're missing parenthesis around this, so you'll get a SyntaxError
if (isEven(n)) {
You're also missing a () after testCondition, as pointed out by Frits van Campen.
Just some minor syntax issues, try this:
function isEven(value) {
//value = Number(value);
if (value%2 == 0){
return true;
}else{
return false;
}
}
function testCondition() {
if (isEven(n)) {
//do something
} else {
//do something else
}
}