Function within another function in javascript - javascript

I was trying to call another function inside function iseven(n):
function iseven(n) {
function remainder(n) {
if (n%2==0) {
return true;
} else {
return false;
}
}
}
console.log(iseven(4));
It returns undefined.
a right way to do this:
function a(x) { // <-- function
function b(y) { // <-- inner function
return x + y; // <-- use variables from outer scope
}
return b; // <-- you can even return a function.
}
console.log(a(3)(4));
nested functions JS

You want something more like this
function iseven(n) {
if (n%2==0) {
return true; }
else {
return false;
}
}
console.log(iseven(4));
And something a bit more succinct:
function isEven(n) {
return n % 2 === 0;
}
Not quite sure why the original was structure that way..

Try
function iseven(n) { return n % 2 === 0; }

Your Main function iseven() does not return anything. Based on your code it should return false. Here's the fix:
function iseven(n) {
function remainder(n) {
if (n%2==0) {
return true;
}
else {
return false;
}
}
//iseven() should return something
return false;
}
console.log(iseven(4));

Related

Can I return 2 different data types in javascript function

Given 2 conditions I want to either return an object or bool as follows:
function foo() {
if (a==b) {
return {
bool: bool,
string: "string"
};
} else {
return false;
}
}
function callFoo() {
var obj = foo();
}
This seems to be failing for me. IE won't hit breakpoints for some reason either so its hard for me to trouble shoot.
Regards.
Yes, in Javascript you can return different types from the same function. Take a look at the following example:
function getAnswer(input) {
if (input == "foo") {
return "bar";
}
else if (input == true) {
return true;
}
else if (input == 100) {
return {
type:"number",
value:100,
}
}
return false;
}
// Different inputs
console.log(getAnswer("foo"));
console.log(getAnswer(true));
console.log(getAnswer(100));
console.log(getAnswer(false));
I think I understood you, but I am not sure. Fix me if I'm wrong:
function foo(a, b) {
if (a==b) {
return {
bool: true,
string: "string"
};
}
return false;
}
function callFoo(a, b) {
var obj = foo(a, b);
console.log(obj);
}
callFoo(1, 1);
callFoo(1, 2);

Stop function when another function return javascript

Let's say I have 3 functions like below:
function1() {
a = 1;
b = 2;
function2(a);
function3(b);
}
function2(a) {
if (a == 1) {
return alert("test")
}
}
function3(a) {
if (a == 2) {
return alert("test")
}
}
Now i want to prevent function3() from running in function1() if function2() returns something. How is that possible?
If function2 alert comes, then stop calling function3. You cannot return alert() as that would result in undefined. Instead let alert complete and then return. And use that return as a condition for the next function call. Below is the updated code:
function function1() {
a = 1;
b = 2;
if (!function2(a)) {
function3(b);
}
}
function function2(a) {
if (a == 1) {
alert("function2 alert");
return true;
}
return false;
}
function function3(a) {
if (a == 2) {
alert("function3 alert");
return true;
}
return false;
}
function1();
Try as follows if function2 return something if block will trigger. Then it will run function3
function1(){
a = 1;
b= 2;
if (!function2(a)) {
function3(b)
}
}
function2(a){
return a ==1
}
function3(a){
return a ==2
}

How to keep a count of how many times a function which returns itself has been called?

My question is, if I have a function like this
function f(a) {
if (a == undefined) {
alert(1)
return f
} else {
alert(2)
}
}
and I call it like this, for example, f()()()()('123'), how can I keep track of how many times f was called?
Edit:
I had a play around and worked out a solution:
function f(a) {
if (!f.count) {
f.count = 0;
}
if (a == undefined) {
alert(1);
++f.count;
return f;
} else {
f.count = 0;
alert(2);
}
}
alert(1) and alert(2) are essentially placeholders for the moment. The count variable would be used in the else section in the actual function.
Thank you all for your help.
Rather than pollute the global space, attach the counter to the function itself.
function f(a)
{
++f.counter;
if (a) {
doAThing();
return f;
} else
doAnotherThing();
}
f.counter = 0;
Now you can access the number of calls by evaluating f.counter at any point.
Just create a count variable outside of the function scope and whenever you enter the function, you increase its value.
var count = 0;
function f(a) {
count++;
if (!a) {
alert(1);
return f;
} else {
alert(2);
}
}
By "wrapper function) do you mean "closure"?
var f = (function (a) {
var count = 0;
return function () {
console.log(count);
count += 1;
if (!a) {
alert(1);
return f;
} else {
alert(2);
}
}
})();

Return functions recursively to form nested functions - Javascript

I'm trying to build a function in JS that has a return composed of different nested functions based on a parameter passed by the user.
function addA(otherFunction)
{
//gets the result from some base function and modifies it
//e.g. +1
}
function addB(otherFunction)
{
//does the same thing as addA, except different values. Consider it a variation of addA.
//eg. -1
}
function constr(input)
{
//based on the chars in input, we will recursively select a new function to be applied.
//the value of this function should be a function
if (...) return addA(constr(shorterInput))
if (*last char) return addA
if (*last char) return addB
if (...) return addB(constr(shorterInput))
}
So far, my script is recognizing addA and and addB as functions. But when it strings two functions together, for example
addB(addA)
The type becomes undefined. Can anybody let me know why it does not register as a function and/or the proper way to return nested functions. Thanks!
Edit: Here is the real code:
function cons(a,b)
{
return function (selector) {
return selector(a,b);
};
}
function a(list)
{
function aHelper(a,b)
{
return a
}
return list(aHelper);
}
function d(list)
{
function dHelper(a,b)
{
return b
}
return list(dHelper);
}
function abc(input)
{
if (input.length==0 || input==null) return null;
var x=input.charAt(input.length-1);
if (x==='a')
{
if (input.length>1)
{
var z=a(abc(input.substr(0,input.length-1)));
return z;
}
return a;
}
if (x==='d')
{
if (input.length>1)
{
var z=d(abc(input.substr(0,input.length-1)));
return z;
}
return d;
}
}
function show(list) {
var sval;
if (list == null) return '()';
else if (typeof list!='string')
{
sval = '(' + show(a(list)) + ' ' + show(d(list)) + ')';
}
else
{
sval=list;
}
return sval;
}
var func=abc('ad');
var func2=abc('a');
var list=cons('a',cons('b','c'));
console.log(typeof func);
console.log(typeof func2);
console.log(typeof list);
console.log(typeof func2(list));
console.log(typeof func(list));
Your function abc is supposed to return a function that can process lists, like a or d. However, you match that signature only in 2 out of 7 cases:
return a, return d are fine
return null - that's not a callable value
z = d(…); return z does return a list
z = a(…); return a does return an element of the list (of whatever type)
d(abc(…)) and a(abc(…)) use abc as if it would return a list
A correct implementation would look like this:
function abc(directions) {
if (directions.length == 0) {
return function id(list) { return list; }; // a function that does nothing
}
var f = directions[0] == 'a' ? car : cdr; // ignoring other values, you might also throw an error
var processRest = abc(input.slice(1));
return function(list) { // make a function to process a list
var z = f(list); // do the current operation
return processRest(z); // do the rest of operations
};
}
Or even better/shorter with the help of higher-order function composition:
function id(x) { return x; }
function compose(f, g) {
if (f == id) return g;
if (g == id) return f;
return function(x) { return f(g(x)); };
}
function abc(dirs) {
return !dirs.length ? id : compose(abc(dirs.slice(1)), dirs[0]=='a'?car:cdr);
}

JS function name interferes with object property

I'm trying to build a polyfill for getting the actual scrollposition.
function getScLPos() {
if (self.pageXOffset) return self.pageXOffset;
if (document.documentElement && document.documentElement.scrollLeft)
return document.documentElement.scrollLeft;
if (document.body.scrollLeft) return document.body.scrollLeft;
return 0;
}
But as it goes through the same condition checking procedure every function call.
I wanted to optimize the function by assigning a function reference to a global variable when it is called for the first time. Afterwards it reads the global variable (containing the funtion) and executes the function, but actually it doesnt work. When i call getScLPos() it still returns a function.
How ca I make getScLPos return an integer value?
Edit: typeof pageXOffset() says "function".
var getScLCallback = null;
function getScLPos() {
if (getScLCallback != null) {
return getScLCallback();
} else {
if (self.pageXOffset) {
getScLCallback = pageXOffset;
//says "function() {...}"
console.log(getScLCallback());
return getScLCallback();
} else if { ... }
} else {
return 0;
}
}
}
function pageXOffset() {
return self.pageXOffset;
}
Yes, function references exist in JavaScript. Functions are just objects. The thing you are doing wrong is you are assigning the result of the function to the variable, instead of assigning the function itself. To assign a function to a variable, simply use the function name, without the () at the end.
So, instead of:
getScLCallback = docScrollLeft();
do this:
getScLCallback = docScrollLeft;
Here is your updated code:
var getScLCallback = null;
function getScLPos() {
if (getScLCallback != null) {
return getScLCallback();
} else {
if (self.pageXOffset) {
// REMOVED the parentheses
getScLCallback = pageXOffset;
//says "function() {...}"
console.log(getScLCallback());
return getScLCallback();
} else if (document.documentElement && document.documentElement.scrollLeft) {
// REMOVED the parentheses
getScLCallback = docScrollLeft;
return getScLCallback();
} else if (document.body.scrollLeft) {
// REMOVED the parentheses
getScLCallback = bodyScrollLeft;
return getScLCallback();
} else {
return 0;
}
}
}
function pageXOffset() {
return self.pageXOffset;
}
function docScrollLeft() {
return document.documentElement.scrollLeft;
}
function bodyScrollLeft() {
return document.body.scrollLeft;
}
I actually made it work by renaming the pageXOffset function to pXOffset.
pageXOffset actually interfered with self.pageXOffset

Categories