Stop function when another function return javascript - 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
}

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);

Function within another function in 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));

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);
}
}
})();

how to implement multiple javascript function in onSubmit() form

I have 4 js function:validateDate,validateRoom,validateCardDate and validateCard
now on submit of form I want to execute all of them.OR I want to execute 2nd if 1st is true such for all. I have implement some advise like:
return fun1() && fun2() && fun3(),
return fun1(); fun2(),
and made wrapper function too.. but could not get success.
UPDATE:MY CODE IS:
is their any mistake in code? every attempt has been failed so far.
function validateDate() {
var x = document.forms["form"]["checkin"].value;
var y = document.forms["form"]["checkout"].value;
if (x == y) {
alert("checkout date should be different from checkin");
return false;
}else if(x > y){
alert("checkout date should be greater");
return false;
}else{return true;}
}
function validateRoom() {
var a = document.forms["form"]["singleroom"].value;
var b = document.forms["form"]["doubleroom"].value;
var c = document.forms["form"]["tripleroom"].value;
if (a == 0 && b==0 && c==0) {
alert("Please select atleast one field");
return false;
}else{return true;}
}
function validateCardDate() {
var month = document.forms["form"]["month"].value;
var year = document.forms["form"]["year"].value;
var today = new Date();
if(year < today.getFullYear()){
alert("Card is expired");
return false;
}else if(year == today.getFullYear()){
if(month <= today.getMonth())
alert("Card is expired");
return false;
} else {return true;}
}
function validateCard() {
var cardType = document.forms["card"]["cardType"].value;
var cardNumber = document.forms["card"]["cardNumber"].value;
if(cardType == "visa"){
var cardno = /^(?:4[0-9]{12}(?:[0-9]{3})?)$/;
if(cardNumber.match(cardno))
{
return true;
}
else
{
alert("Not a valid Visa credit card number!");
return false;
}
}else if(cardType == "americanexpress"){
var cardno = /^(?:3[47][0-9]{13})$/;
if(cardNumber.match(cardno))
{
return true;
}
else
{
alert("Not a valid Amercican Express credit card number!");
return false;
}
}else if(cardType == "mastercard"){
var cardno = /^(?:5[1-5][0-9]{14})$/;
if(cardNumber.match(cardno))
{
return true;
}
else
{
alert("Not a valid mastercard credit card number!");
return false;
}
}
else if(cardType == "jcb"){
var cardno = /^(?:(?:2131|1800|35\d{3})\d{11})$/;
if(cardNumber.match(cardno))
{
return true;
}
else
{
alert("Not a valid JCB credit card number!");
return false;
}
}
}
Simply do:
function main(){
//functions to exexute.
}
Then do:
onsubmit="main()"
If you want to execute the second if the first is true then
The first function must return true
if(main()){if(//otherfunction){}}
Try this:
if (func1()){
if(func2()){
if(func3()){
return func4()
}else{
return false;
}
}else{
return false
}
}else{
return false
}
Every functions func1...func4 should be return a false or true value.
Create a new function which has all those four functions inside it
Example:
function ParentFunction() {
validateDate()
validateRoom()
validateCardDate()
validateCard()
}
An onSubmit call the ParentFunction(). This way you can even use arguments and decision controls to run those functions in any sequence you like.
UPDATE
Try this:
var validateDate = function () {
// Statements
return true // if conditions are what you want
}
var validateRoom = function () {
// Statements
return true // if conditions are what you want
}
var validateCardDate = function () {
// Statements
return true // if conditions are what you want
}
var validateCard = function () {
// Statements
return true // if conditions are what you want
}
function ParentFunction() {
if (validateDate() == true) {
if (validateRoom() == true) {
if (validateCardDate() == true) {
if (validateCard() == true) {
return true
}
}
}
}
return false
}
Hope it helps!

javascript: why the return value is possible to be false

function test() {
alert(1);
return "hello";
}
Function.prototype.before = function (func) {
var __bself = this;
return function () {
if (func.apply(this, arguments) == false)
return false;
return __bself.apply(__bself, arguments);
}
};
test.before(function (){
alert(2);
})();
What is the meaning of if (func.apply(this, arguments) == false)?
I don't think the function will return false.
Functions can return any value. That includes false.
If your functions don't return false then the code inside that conditional will never run. So you can remove it, if it annoys you for some reason.
Here is an example with a function which returns false:
function test() { // This function is never called
console.log(1);
return "hello";
}
Function.prototype.before = function (func) {
var __bself = this;
return function () {
if (func.apply(this, arguments) == false){
return false;
}
return __bself.apply(__bself, arguments);
}
};
test.before(function (){
console.log(2);
return false;
})();

Categories