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

Related

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

Patterns for dealing with variadic arguments in JavaScript [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 usually do something like the following when dealing with variadic arguments in JavaScript:
var f = function() {
var args = Array.prototype.slice.call(arguments, 0);
// ...
return something;
};
But what about doing instead:
var f = function() {
return (function(self, args) {
// ...
return something;
}(this, Array.prototype.slice.call(arguments, 0)));
};
I couldn't readily find anything addressing the pattern above, so I decided to post it here. Before I start experimenting with it, I would like to know if there are any flaws or risks I could be missing when using the rather unusual patterns described above.
I have found the pattern to be slightly less readable than the first, but it lets me write longer one-liners without declaring variables so I am growing fond of it.
Object.defineProperty(RegExp, 'join', {
value: function() {
return (function(regexes, last) {
return RegExp(regexes.map(function(item) {
return item.source;
}).reduce(function(prev, next) {
return prev + next;
}), last.slice(last.lastIndexOf('/') + 1));
}(Array.prototype.slice.call(arguments, 0),
arguments[arguments.length - 1]+''));
}, enumerable: false
});

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

Add parameter to function dynamically [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
// set Process
i18n.setProcess(function() {diFunctions.getI18n('http://localhost/service/i18n/page?lang=eng&group=staff') });
// Setter and getter
this.setProcess = function( opProcess ) { //here param opProcess is function with parameters see i18n.setProcess() line of code
if( opProcess == undefined)
throw "Process is undefined";
if( $.isFunction(opProcess) == false )
throw "Process is not a function"
process = opProcess;
};
this.getProcess = function() {
return process;
};
See how i18n.setProcess passes a function with param as a parameter to setProcess.
Now i what i want in SetProcess is function() {diFunctions.getI18n('http://localhost/service/i18n/page?lang=eng&group=staff',**id**) // id is added dynamically to the function itself which was passed as parameter to setProcess
Problem - I want to add id dynamically(defined in my class variable always accesible by id) on set process with addition to functions parameter(Url,etc,etc,id). Functions parameters can grow but the id should be added last as a parameter?
Tried quite a few solutions but didnot work out? Check here
This is what the arguments object is for..
function foo() {
var lastArg = arguments.length ? arguments[arguments.length - 1] : 'default';
return lastArg;
}
foo(); // "default"
foo(1); // 1
foo(1, 2); // 2
If you want to write a function similar to bind which only sticks arguments on the end then you could do
function appendArguments(fn) {
var slice = Array.prototype.slice.call.bind(Array.prototype.slice),
args = slice(arguments, 1);
return function () {
return fn.apply(this, slice(arguments).concat(args));
};
}
now
var bar = appendArguments(foo, 3);
bar(); // 3
bar(4, 5); // 3 (because it's calling `foo(4, 5, 3)`

Categories