Javascript check if parameter contain this keyword [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 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");

Related

Can this If statement be written in one line? [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 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);

Why return a function instead of just running that function again (recursion)? [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 4 years ago.
Improve this question
If I want to run my function again inside that same function when a certain condition is met, what's the difference between returning that function VS just calling that function again.
function myFunc(param) {
if (param === "3") {
return myFunc(param);
}
}
VS
function myFunc(param) {
if (param === "3") {
myFunc(param);
}
}
If you need to return a result, then you need to return the result of the inner call recursively. For example:
function fact(n) {
if (n === 0) return 1;
return n * fact(n - 1);
}
console.log(fact(5));
But if the recursive function accomplishes what it needs to only inside the function - it's all side-effects from inside - then there's no need to return:
function addSpans(container, n) {
const span = container.appendChild(document.createElement('span'));
span.textContent = n;
if (n >= 1) addSpans(span, n - 1);
}
addSpans(document.body, 3);
return myFunc() does three things: (1) execute the function myFunc(), (2) when 1 finishes, take the result value and assign it as the return value of the current function, and (3) terminate the current function.
Calling myFunc() only does (1). That is the difference.

Javascript not executing if statements sequentially [closed]

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.

Javascript function not called when used with IF statement [closed]

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

Short Javascript code snippet involving if / else not being evaluated correctly [closed]

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
}
}

Categories