How to access a JavaScript function parameter in another function - javascript

I have two different JavaScript functions in same file, like this:
function functionOne(a)
{
if(a)
{
return true;
}
else
{
return false;
}
}
function functionTwo(b)
{
//I want to access if condition parameter a here
}
I want to access functionOne if condition parameter a in functionTwo.

function functionTwo(b)
{
if(functionOne(b))
return true;
else
return false;
}

The following are the two ways....
Option 1:
<script>
var newVar = "";
function functionOne(a)
{
if(a)
{
newVar = true;
return true;
}
else
{
return false;
}
}
function functionTwo(b)
{
//user newVar vairalbe to get the value
//i want to access if condition parameter a here
}
Option 2:
<script>
function functionOne(a)
{
if(a)
{
return a;
}
else
{
return false;
}
}
function functionTwo(b)
{
// call functionOne(a) to get the value;
//i want to access if condition parameter a here
}
</script>

Related

How can I pass a Regular Expression through a function?

I want to validate some fields passing a different pattern to a validation function.
send.addEventListener("click", function (event) {
let pattern = /^[A-Za-zÁ-Úá-ú\s]{3,15}$/;
let nameIsVal = regexValidator(pattern);
if (nameIsVal) {
return true;
} else {
event.preventDefault();
return false;
}
});
function regexValidator(pattern) {
if (!pattern.test(this.value)) {
return false;
} else {
return true;
}
}
I am assuming that you are in a class. Make sure that the this keyword points to the correct instance in your event handler, either with the .bind() keyword or with an arrow function.
I would register the handler like this:
send.addEventListener('click', (event) => this.checkRegex(event));
Or if your environment doesn't support arrow functions, this should work as well:
send.addEventListener('click', this.checkRegex.bind(this, event));
Then I would add the methods to the class like this:
checkRegex(event) {
let pattern = /^[A-Za-zÁ-Úá-ú\s]{3,15}$/;
let nameIsVal = this.regexValidator(pattern);
if (nameIsVal) {
return true;
} else {
event.preventDefault();
return false;
}
}
regexValidator(pattern) {
if (!pattern.test(this.value)) {
return false;
} else {
return true;
}
}

How to return from a higher function [duplicate]

This question already has an answer here:
Break statement in javascript array map method [duplicate]
(1 answer)
Closed 6 years ago.
For example:
function foo() {
someArray.map(bar) {
if (whatever)
return; // I want to return from the foo function
return bar.something // Here I return to map function
}
}
Here is my current solution:
let flag = false
function foo() {
someArray.map(bar) {
if (whatever){
flag = true
}
return bar.something // Here I return to map function
}
}
if (flag === true) {
return;
}
I'm wondering if there is a better way?
You can't break your map function. But you don't use results of map. If you want to break your map after any condition you can use some array method instead of it.
For example:
let flag = false;
function foo() {
someArray.some(bar) {
if (!whatever){
return false;
}
flag = true;
return true;
}
}
if (flag === true) {
return;
}
Or you can call any function inside some instead of using flag variable
function foo() {
someArray.some(bar) {
if (!whatever){
return false;
}
callYourCodeHere()
return true;
}
}
Or something like this:
function foo() {
const anySuccess = someArray.some(bar) {
return !!whatever;
}
if(anySuccess) {
callYourCodeHere();
}
}

Simple Javascript: How to invoke a function based off user prompt;

I've defined some functions, and I want to get user input to invoke those functions. I have the following code, but can't figure out how to invoke the actual function when I'm using a variable. I assumed below code would work..
thanks!
var someFunctions = {
play: function() {
if (player.stopped()) {
player.play();
} else {
return false;
}
}
var getCommand = function(){
var command = prompt("Please enter a command");
if (!(command in someFunctions)) {
alert('command not recognized');
getCommand();
} else {
command();
}
}
getCommand();
var someFunctions = {
play: function() {
if (player.stopped()) {
player.play();
}
else {
return false;
}
}
};
var getCommand = function(){
var commandInput = prompt("Please enter a command");
var command = someFunctions[commandInput];
if (!command) {
alert('command not recognized');
getCommand();
}
else {
command();
}
};
getCommand();
The reason your code isn't working is because you're missing the closing } of someFunctions.
var someFunctions = {
play: function() {
if (player.stopped()) {
player.play();
} else {
return false;
}
}
} // here
Your call is fine, you call a "variable" function the same way you do a regular one. The only difference between a "variable" function and an ordinary one is you can call the ordinary one before it's declared (if you're in the same scope at least)

Javascript/JQuery: return immediately from PARENT function in call to each() via anon function

For example:
> function foo() {
> jQuery(whatever).each( function() {
return; // this just exits the anonymous function - is there a way to return from foo?
}
);
>
> }
**Correction: Added more detail. Use a flag to allow returning from the PARENT function **
function foo() {
var doreturn = false;
jQuery(whatever).each( function() {
if(youwanttoreturn){
doreturn=1;
return false;
}
});
if(doreturn)return;
}
http://api.jquery.com/each/
"We can stop the loop from within the callback function by returning false."
The function can return false.
edit oh ha ha, the "from foo" was scrolled off the right side :)
To do that, you could use try/catch
function foo() {
try {
jQuery('whatever').each(function() {
if (noMoreFoo()) throw "go";
});
}
catch (flag) {
if (flag === "go") return;
throw flag;
}
}
Not really. This will ghetto do what you want (i think):
function foo() {
var bar=null;
$(whatever).each( function() {
bar="bar";
return false;
});
return bar;
}
var fooResults = foo();
function foo() {
$result = false;
jQuery(whatever).each( function() {
$result = true;
});
// We will reach this point after the loop is over.
return $result;
}

How to call outer function's return from inner function?

I have such code:
function allValid() {
$('input').each(function(index) {
if(something) {
return false;
}
});
return true;
}
which always returns true as return false; affects anonymous inner function. Is there an easy way to call outer function's return?
PS. I am not looking for a workaround, just want to know the answer to original question. If the answer is "not possible" it is fine.
Yeah, store it in a local variable.
function allValid() {
var allGood = true;
$('input').each(function (index) {
if (something) {
allGood = false;
}
});
return allGood;
}
You could also use Array.prototype.some which iterates until finding an element that matches the criteria.
function allValid() {
var inputs = $('input');
if(inputs.toArray().some(function(input){
if(something)
return true;
})) {
return false;
} else {
return true;
}
}
You can also do this with filter:
var anyInvalid = $('input').filter(function(index) {
if (inValidCheck)
return true;
}).length;
This works because 0 is treated as false, but it actually gives you the number of invalid, which you could use this to display "You have 3 invalid entries" or something if you wanted.
If you want to do this efficiently, I think this is the best way:
function allValid() {
elements = $('input')
for (i = 0; i < elements.length; i++) { invalidityCheck(elements[i]) && return false; }
return true;
}
Edit: Although a more JavaScript-y version would probably use exceptions:
function allValid() {
try
$('input').each(function(index)) {
if (something) { throw 'something happened!'; }
});
catch (e) {
if (e == 'something happened!') {
return false;
} else {
throw e;
}
}
return true;
}

Categories