Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm trying to write a form validator. I expect that it will check all the if statements and finally give a true or false result. But it is returning always true as soon as it called.
function validator() {
var ret = true;
fname = document.getElementById('fname').value;
newp = document.getElementById('newp').value;
if (fname == null || fname == "") {
bootbox.alert("Name is empty");
ret = ret && false;
}
if (newp.length > 0) {
bootbox.confirm("Account will be locked",
function(result) {
if (result == true) {
ret = ret && true;
} else {
ret = ret && false;
}
});
}
return ret;
}
The bootbox confirmation dialog will not block execution of that outer "validator" function. That outer function will return while the confirmation dialog is still showing.
Using something like the Bootstrap dialog code, you really cannot make a function like that. The callback function you pass to the confirmation dialog will be invoked, but not until the user interacts with the dialog.
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);
This question already has answers here:
Can you alter a Javascript function after declaring it?
(12 answers)
Closed 6 years ago.
In our ordering system there is an embedded function I have no access to at all. Conveniently there is a spelling error in it so when a user clicks something a popup appears and has grammar issues in it.
Is there a way for me to replace that text or replace the function with a new function that has all the same code but correct spelling?
This is the function I need to edit.. note confirm says 'You selection' not 'Your selection'
I'd prefer to replace the whole thing because I may want to do some other edits but for now I'd like to fix that spelling error.
function showProof()
{
var blnSubmit = false;
var strHid='';
var strSave='';
if (aryTemplateChoices.length == 0)
{
blnSubmit = true;
}
else
{
for (var i=1; i<=aryTemplateChoices.length; i++)
{
strSave = aryTemplateChoices[i-1];
if (strSave=='')strSave='0';
if (document.getElementById('hidctrl'+ i))strHid = document.getElementById('hidctrl'+ i).value;
if (strHid=='')strHid='0';
if ((strSave != '0') && (strHid != strSave))
{
blnSubmit = true;
break;
}
}
}
if (blnSubmit)
{
if (confirm('Your selection has changed, do you want to save?'))
{
document.getElementById('subtype').value = 'proof';
document.getElementById('prevclick').value = '';
document.getElementById('frm1').submit();
}
}
canAddToCart();
//<!--WRITE-->
getQuantityPrice()
//<!--/WRITE-->
loadProof();
}
It doesn't really matter where the original function is and how you access it, as long as you just redefine the function (with the same name and corrected code) at a scope closer to where you want to use it.
Here's an example:
function foo(){
console.log("ORIGINAL foo says hello.");
}
function foo(){
console.log("NEW foo says hello.");
}
// The second declaration is in the same scope as the first, but it comes after the first
// so it overwrites that declaration and the second one is the one that is used.
foo();
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 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
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
}
}