I have this case:
function f1(a) {
a = f2(a);
// do some other stuff
console.log("I don't want the function f1 to log this.")
}
function f2(a) {
if (a == null) {
console.log("enters here")
return;
}
return a;
}
f1(null)
if a is null, I don't want f1 to continue with the console.log(). What can I change to obtain this behavior? (I know I can do this with some booleans but I was wondering if there is another way to solve this)
You could store the last value of a and exit if temp is null.
function f1(a) {
let temp = a;
a = f2(a);
// do some other stuff
if (temp === null) return;
console.log("I don't want the function f1 to log this.");
}
function f2(a) {
if (a == null) {
console.log("enters here");
return;
}
return a;
}
f1(null);
if a is null, I don't want f1 to continue with the console.log()
In that case you have to test the value of a within the f1 function:
function f1(a) {
a = f2(a);
// do some other stuff
if (a == null) return;
console.log("I don't want the function f1 to log this.")
}
function f2(a) {
if (a == null) {
console.log("enters here")
return;
}
return a;
}
f1(null)
just return false
function f1(a) {
a = f2(a);
// do some other stuff
if (!a) return
console.log("I don't want the function f1 to log this.")
}
function f2(a) {
if (a == null) {
console.log("enters here")
return false;
}
return a;
}
f1(null)
Related
I have added these functions and need return something based which function is returning true but it is not working.
//this is function1/
function A() {
return true;
}
function B() {
return true;
}
function C() {
if ({
{
var -customJS - page_type_lookup
}
} === 'product') {
var config = document.querySelector('#product-content > div.product-variations.clearfix > div.attribute.variant-dropdown > div.attribute-values-section > div.label.va-navSectionalOrientation').innerText;
if (config.includes('Sectional Orientation')) {
return true;
} else {
return false;
}
}
}
if (A() === true && B() === true && C() === true) {
return 'A+ Content, RTA Product, Sectional Configurator';
} else if (A() === true && B() === true) {
return 'A+ Content, RTA Product';
} else if (B() === true && C() === true) {
return 'RTA Product, Sectional Configurator';
} else if (C() === true && A() === true) {
return 'Sectional Configurator, A+ Content';
} else if (A() === true) {
return 'A+ Content';
} else if (B() === true) {
return 'RTA Product';
} else {
return 'Sectional Configurator';
}
}
If you have more than one function and a data set which reflects the wanted value of each flag, you could take an array for the functions retun values and another for the strings which are filterd and joined for the result.
const
flags = [a(), b(), c()],
result = ['A+ Content', 'RTA Product', 'Sectional Configurator']
.filter((_, i) => flags[i])
.join(', ');
I am creating code similar to this:
function foo(type,callback) {
if(type === "a") {
function text_a() {
return "a";
}
}else if(type === "b") {
function text_b() {
return "b";
}
}else if(type === "c") {
function text_c() {
return "c";
}
}
if(typeof callback === "function") callback();
}
foo("a",function() {
console.log(text_a());
});
I want the function text_a() inside foo() to only be available when the type is equal to the relevant type and is called inside the foo() function. However this does not work. I keep getting an error message saying that text_a() is not defined, which makes no sense because when the foo function is called it should also declare the text_a function if the type is "a".
Just save a reference to the function and pass it to the callback..
eg.
function foo(type,callback) {
var fn;
if(type === "a") {
fn = function text_a() {
return "a";
}
}else if(type === "b") {
fn = function text_b() {
return "b";
}
}else if(type === "c") {
fn = function text_c() {
return "c";
}
}
if(typeof callback === "function") callback(fn);
}
foo("a",function(fn) {
console.log(fn());
});
foo("b",function(fn) {
console.log(fn());
});
In some places of my program I use a check that holds the script if the data doesn't match:
const a = 1;
//...
//...
if (param1 != 1 && param2 != 3) {
return; // stop script
}
//...
//...
if (param1 != 1 && param2 != 3) {
return; // stop script
}
//...
//...
So i create function:
function checkIfOk(param1, param2)
{
if (param1 != 1 && param2 != 3) {
return; // stop script
}
}
But now:
const a = 1;
//...
//...
checkIfOk(param1, param2);
//...
//...
checkIfOk(param1, param2);
//...
//...
Doesn't pause the function at error.
const a = 1;
//...
//...
return checkIfOk(param1, param2);
//...
//...
return checkIfOk(param1, param2);
//...
//...
This stops the function in any situation.
I don't want use throw errors because I use it in functions, so program should still running.
Here's a quicker way to implement what you want:
function checkIfOk(param1, param2) {
return (param1 != 1 && param2 != 3);
}
if (checkIfOk(param1, param2))
return;
I guess semantically it should be checkIfNotOk(), but eh...
You need to return a value (e.g. false and true), and only stop for example when returning false
e.g.
function checkIfOk(param1, param2)
{
if (param1 != 1 && param2 != 3) {
return false; // stop script
}
return true; // OK!!
}
if(!checkIfOk(param1, param2))
return; //only return if checkIfOk returns false
I have a function like so:
function contains(needle,haystack) {
return $.each(haystack,function(i,v) {
if(v == needle) {
console.log('true, so return');
return true;
}
if(typeof v == 'object') {
return app.contains(needle,v);
}
console.log('I have not yet returned');
return false;
});
}
When I pass something into it (let's say an object of length 2, with one item matching) the console shows:
true, so return
I have not yet returned
I have not yet returned
Why is this? How do I fix it?
Returning true/undefined from the each callback will continue executing the next iteration.
What you need to do is to use a flag and if a match is found then set the flag to true and return false so that rest of the iterations will be ignored
function contains(needle, haystack) {
var result = false;
$.each(haystack, function (i, v) {
if (v == needle) {
console.log('true, so return');
result = true;
return false;
}
if (typeof v == 'object') {
if (app.contains(needle, v)) {
result = true;
return false;
}
}
console.log('I have not yet returned');
});
return result;
}
It is because you return only from inner function of each $.each iteration.
To break $.each loop you need return false, so return !result will return false if and only if result if found
function contains(needle,haystack) {
var result = false;
$.each(haystack,function(i,v) {
if(v == needle) {
result = true;
return !result;
}
if(typeof v == 'object') {
result = contains(needle,v);
return !result;
}
});
return result;
}
http://jsfiddle.net/8dggq7ds/2/
Or shorter version
function contains(needle,haystack) {
var result = false;
$.each(haystack,function(i,v) {
return !(result = (typeof v === 'object') ? contains(needle,v) : v == needle);
});
return result;
}
Moreover, recursion call app.contains() change to contains().
I can't figure out why this function code will not run? Here is my code.
function isEven(n) {
if (n%2 == 0) {
return true;
};
else {
return false;
};
};
console.log(isEven(50));
I am getting an error message with the "else" statement.
Bit handy on the semi-colons
Should be
function isEven(n) {
if (n%2 == 0) {
return true;
}
else {
return false;
}
}
console.log(isEven(50));
Could even use
function isEven(n) {
return (n%2 == 0);
}
console.log(isEven(50));