Declaring forEach globally and using it in functions
I have a JSON named employees of 10 elements in it, like below
{
"name" : "Prem",
"dob" : "10-10-1992"
},
With three buttons, each has a specific function
document.getElementById('btn-25').addEventListener('click',ageCategory25)
document.getElementById('btn-50').addEventListener('click',ageCategory50)
document.getElementById('btn-100').addEventListener('click',ageCategory100)
const output = document.getElementById('result2')
function ageCategory25(){
output.innerHTML =''
employees.forEach(function(employee){
const ageFilter = getAge(employee.dob)
if(ageFilter < 25 ){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
})
}
function ageCategory50(){
output.innerHTML =''
employees.forEach(function(employee){
const ageFilter = getAge(employee.dob)
if(ageFilter >= 25 && ageFilter<=50){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
})
}
function ageCategory100(){
output.innerHTML =''
employees.forEach(function(employee){
const ageFilter = getAge(employee.dob)
if(ageFilter >= 50 && ageFilter<=100){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
})
}
function getAge(date){
const currentAge = Math.floor((new Date() - new Date(date).getTime())/31556925994)
return currentAge
}
The above code works correctly, i'm able to get the result as per the age mentioned in the buttons.
But, I'm declaring forEach loop in all functions, is there a way to declare forEach once globally and use it in all functions.
Thanks in advance
javascript json foreach
add a comment |
I have a JSON named employees of 10 elements in it, like below
{
"name" : "Prem",
"dob" : "10-10-1992"
},
With three buttons, each has a specific function
document.getElementById('btn-25').addEventListener('click',ageCategory25)
document.getElementById('btn-50').addEventListener('click',ageCategory50)
document.getElementById('btn-100').addEventListener('click',ageCategory100)
const output = document.getElementById('result2')
function ageCategory25(){
output.innerHTML =''
employees.forEach(function(employee){
const ageFilter = getAge(employee.dob)
if(ageFilter < 25 ){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
})
}
function ageCategory50(){
output.innerHTML =''
employees.forEach(function(employee){
const ageFilter = getAge(employee.dob)
if(ageFilter >= 25 && ageFilter<=50){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
})
}
function ageCategory100(){
output.innerHTML =''
employees.forEach(function(employee){
const ageFilter = getAge(employee.dob)
if(ageFilter >= 50 && ageFilter<=100){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
})
}
function getAge(date){
const currentAge = Math.floor((new Date() - new Date(date).getTime())/31556925994)
return currentAge
}
The above code works correctly, i'm able to get the result as per the age mentioned in the buttons.
But, I'm declaring forEach loop in all functions, is there a way to declare forEach once globally and use it in all functions.
Thanks in advance
javascript json foreach
1
yes, you could have one function called from all your buttons, if you just tell it the thing it needs to do differently each time. In this case it seems the parameters of the if statement appear to be the only thing which varies, so those values would be the things to use as the input parameters for your single function. (P.S. the ability to abstract your code like this and pick out the things which are variable and the things which are the same each time is a key skill if you want to be able to write lean, non-repetitive code).
– ADyson
Nov 13 '18 at 14:58
@ADyson Thank you so much for your valuable note :)
– PremKumar
Nov 13 '18 at 15:24
add a comment |
I have a JSON named employees of 10 elements in it, like below
{
"name" : "Prem",
"dob" : "10-10-1992"
},
With three buttons, each has a specific function
document.getElementById('btn-25').addEventListener('click',ageCategory25)
document.getElementById('btn-50').addEventListener('click',ageCategory50)
document.getElementById('btn-100').addEventListener('click',ageCategory100)
const output = document.getElementById('result2')
function ageCategory25(){
output.innerHTML =''
employees.forEach(function(employee){
const ageFilter = getAge(employee.dob)
if(ageFilter < 25 ){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
})
}
function ageCategory50(){
output.innerHTML =''
employees.forEach(function(employee){
const ageFilter = getAge(employee.dob)
if(ageFilter >= 25 && ageFilter<=50){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
})
}
function ageCategory100(){
output.innerHTML =''
employees.forEach(function(employee){
const ageFilter = getAge(employee.dob)
if(ageFilter >= 50 && ageFilter<=100){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
})
}
function getAge(date){
const currentAge = Math.floor((new Date() - new Date(date).getTime())/31556925994)
return currentAge
}
The above code works correctly, i'm able to get the result as per the age mentioned in the buttons.
But, I'm declaring forEach loop in all functions, is there a way to declare forEach once globally and use it in all functions.
Thanks in advance
javascript json foreach
I have a JSON named employees of 10 elements in it, like below
{
"name" : "Prem",
"dob" : "10-10-1992"
},
With three buttons, each has a specific function
document.getElementById('btn-25').addEventListener('click',ageCategory25)
document.getElementById('btn-50').addEventListener('click',ageCategory50)
document.getElementById('btn-100').addEventListener('click',ageCategory100)
const output = document.getElementById('result2')
function ageCategory25(){
output.innerHTML =''
employees.forEach(function(employee){
const ageFilter = getAge(employee.dob)
if(ageFilter < 25 ){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
})
}
function ageCategory50(){
output.innerHTML =''
employees.forEach(function(employee){
const ageFilter = getAge(employee.dob)
if(ageFilter >= 25 && ageFilter<=50){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
})
}
function ageCategory100(){
output.innerHTML =''
employees.forEach(function(employee){
const ageFilter = getAge(employee.dob)
if(ageFilter >= 50 && ageFilter<=100){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
})
}
function getAge(date){
const currentAge = Math.floor((new Date() - new Date(date).getTime())/31556925994)
return currentAge
}
The above code works correctly, i'm able to get the result as per the age mentioned in the buttons.
But, I'm declaring forEach loop in all functions, is there a way to declare forEach once globally and use it in all functions.
Thanks in advance
javascript json foreach
javascript json foreach
asked Nov 13 '18 at 14:55
PremKumarPremKumar
16413
16413
1
yes, you could have one function called from all your buttons, if you just tell it the thing it needs to do differently each time. In this case it seems the parameters of the if statement appear to be the only thing which varies, so those values would be the things to use as the input parameters for your single function. (P.S. the ability to abstract your code like this and pick out the things which are variable and the things which are the same each time is a key skill if you want to be able to write lean, non-repetitive code).
– ADyson
Nov 13 '18 at 14:58
@ADyson Thank you so much for your valuable note :)
– PremKumar
Nov 13 '18 at 15:24
add a comment |
1
yes, you could have one function called from all your buttons, if you just tell it the thing it needs to do differently each time. In this case it seems the parameters of the if statement appear to be the only thing which varies, so those values would be the things to use as the input parameters for your single function. (P.S. the ability to abstract your code like this and pick out the things which are variable and the things which are the same each time is a key skill if you want to be able to write lean, non-repetitive code).
– ADyson
Nov 13 '18 at 14:58
@ADyson Thank you so much for your valuable note :)
– PremKumar
Nov 13 '18 at 15:24
1
1
yes, you could have one function called from all your buttons, if you just tell it the thing it needs to do differently each time. In this case it seems the parameters of the if statement appear to be the only thing which varies, so those values would be the things to use as the input parameters for your single function. (P.S. the ability to abstract your code like this and pick out the things which are variable and the things which are the same each time is a key skill if you want to be able to write lean, non-repetitive code).
– ADyson
Nov 13 '18 at 14:58
yes, you could have one function called from all your buttons, if you just tell it the thing it needs to do differently each time. In this case it seems the parameters of the if statement appear to be the only thing which varies, so those values would be the things to use as the input parameters for your single function. (P.S. the ability to abstract your code like this and pick out the things which are variable and the things which are the same each time is a key skill if you want to be able to write lean, non-repetitive code).
– ADyson
Nov 13 '18 at 14:58
@ADyson Thank you so much for your valuable note :)
– PremKumar
Nov 13 '18 at 15:24
@ADyson Thank you so much for your valuable note :)
– PremKumar
Nov 13 '18 at 15:24
add a comment |
2 Answers
2
active
oldest
votes
You can receive 25,50,100 in a function.
document.getElementById('btn-25').addEventListener('click',()=>ageCategory(25))
document.getElementById('btn-50').addEventListener('click',()=>ageCategory(50))
document.getElementById('btn-100').addEventListener('click',()=>ageCategory(100))
function ageCategory(n){
output.innerHTML =''
employees.forEach(function(employee){
const ageFilter = getAge(employee.dob)
if(n === 25 && ageFilter < 25 ){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
else if(n === 50 && ageFilter >= 25 && ageFilter<=50){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
else if(n === 100 && ageFilter >= 50 && ageFilter<=100){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
})
}
UPDATED:
I think it can be a little better:
function ageCategory(n) {
output.innerHTML = '';
let filter;
if (n === 25) { filter = (x) => (x < 25); }
else if (n === 50) { filter = (x) => (x >= 25 && x <= 50) }
else if (n === 100) { filter = (x) => (x >= 50 && x<=100) }
employees.forEach(function(employee){
const ageFilter = getAge(employee.dob);
if(filter(ageFilter)){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
})
}
Thank you so much, the one I was expecting. :-)
– PremKumar
Nov 13 '18 at 15:21
1
I've updated the code.
– eag845
Nov 13 '18 at 15:50
Great, thank you :)
– PremKumar
Nov 13 '18 at 15:59
add a comment |
You can create the function like:
function ageCategory(age){
output.innerHTML =''
employees.forEach(function(employee){
const ageFilter = getAge(employee.dob)
if(age == 25 && ageFilter < 25 ){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
else if(age == 50 && ageFilter >= 25 && ageFilter <= 50){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
else if(age == 100 && ageFilter >= 50 && ageFilter <= 100){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
}
}
Then use the function as call back of addEventListener()
by passing the age like the following way:
document.getElementById('btn-25').addEventListener('click', ageCategory(25));
document.getElementById('btn-50').addEventListener('click', ageCategory(50));
document.getElementById('btn-100').addEventListener('click', ageCategory(100));
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283735%2fdeclaring-foreach-globally-and-using-it-in-functions%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can receive 25,50,100 in a function.
document.getElementById('btn-25').addEventListener('click',()=>ageCategory(25))
document.getElementById('btn-50').addEventListener('click',()=>ageCategory(50))
document.getElementById('btn-100').addEventListener('click',()=>ageCategory(100))
function ageCategory(n){
output.innerHTML =''
employees.forEach(function(employee){
const ageFilter = getAge(employee.dob)
if(n === 25 && ageFilter < 25 ){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
else if(n === 50 && ageFilter >= 25 && ageFilter<=50){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
else if(n === 100 && ageFilter >= 50 && ageFilter<=100){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
})
}
UPDATED:
I think it can be a little better:
function ageCategory(n) {
output.innerHTML = '';
let filter;
if (n === 25) { filter = (x) => (x < 25); }
else if (n === 50) { filter = (x) => (x >= 25 && x <= 50) }
else if (n === 100) { filter = (x) => (x >= 50 && x<=100) }
employees.forEach(function(employee){
const ageFilter = getAge(employee.dob);
if(filter(ageFilter)){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
})
}
Thank you so much, the one I was expecting. :-)
– PremKumar
Nov 13 '18 at 15:21
1
I've updated the code.
– eag845
Nov 13 '18 at 15:50
Great, thank you :)
– PremKumar
Nov 13 '18 at 15:59
add a comment |
You can receive 25,50,100 in a function.
document.getElementById('btn-25').addEventListener('click',()=>ageCategory(25))
document.getElementById('btn-50').addEventListener('click',()=>ageCategory(50))
document.getElementById('btn-100').addEventListener('click',()=>ageCategory(100))
function ageCategory(n){
output.innerHTML =''
employees.forEach(function(employee){
const ageFilter = getAge(employee.dob)
if(n === 25 && ageFilter < 25 ){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
else if(n === 50 && ageFilter >= 25 && ageFilter<=50){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
else if(n === 100 && ageFilter >= 50 && ageFilter<=100){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
})
}
UPDATED:
I think it can be a little better:
function ageCategory(n) {
output.innerHTML = '';
let filter;
if (n === 25) { filter = (x) => (x < 25); }
else if (n === 50) { filter = (x) => (x >= 25 && x <= 50) }
else if (n === 100) { filter = (x) => (x >= 50 && x<=100) }
employees.forEach(function(employee){
const ageFilter = getAge(employee.dob);
if(filter(ageFilter)){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
})
}
Thank you so much, the one I was expecting. :-)
– PremKumar
Nov 13 '18 at 15:21
1
I've updated the code.
– eag845
Nov 13 '18 at 15:50
Great, thank you :)
– PremKumar
Nov 13 '18 at 15:59
add a comment |
You can receive 25,50,100 in a function.
document.getElementById('btn-25').addEventListener('click',()=>ageCategory(25))
document.getElementById('btn-50').addEventListener('click',()=>ageCategory(50))
document.getElementById('btn-100').addEventListener('click',()=>ageCategory(100))
function ageCategory(n){
output.innerHTML =''
employees.forEach(function(employee){
const ageFilter = getAge(employee.dob)
if(n === 25 && ageFilter < 25 ){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
else if(n === 50 && ageFilter >= 25 && ageFilter<=50){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
else if(n === 100 && ageFilter >= 50 && ageFilter<=100){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
})
}
UPDATED:
I think it can be a little better:
function ageCategory(n) {
output.innerHTML = '';
let filter;
if (n === 25) { filter = (x) => (x < 25); }
else if (n === 50) { filter = (x) => (x >= 25 && x <= 50) }
else if (n === 100) { filter = (x) => (x >= 50 && x<=100) }
employees.forEach(function(employee){
const ageFilter = getAge(employee.dob);
if(filter(ageFilter)){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
})
}
You can receive 25,50,100 in a function.
document.getElementById('btn-25').addEventListener('click',()=>ageCategory(25))
document.getElementById('btn-50').addEventListener('click',()=>ageCategory(50))
document.getElementById('btn-100').addEventListener('click',()=>ageCategory(100))
function ageCategory(n){
output.innerHTML =''
employees.forEach(function(employee){
const ageFilter = getAge(employee.dob)
if(n === 25 && ageFilter < 25 ){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
else if(n === 50 && ageFilter >= 25 && ageFilter<=50){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
else if(n === 100 && ageFilter >= 50 && ageFilter<=100){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
})
}
UPDATED:
I think it can be a little better:
function ageCategory(n) {
output.innerHTML = '';
let filter;
if (n === 25) { filter = (x) => (x < 25); }
else if (n === 50) { filter = (x) => (x >= 25 && x <= 50) }
else if (n === 100) { filter = (x) => (x >= 50 && x<=100) }
employees.forEach(function(employee){
const ageFilter = getAge(employee.dob);
if(filter(ageFilter)){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
})
}
edited Nov 13 '18 at 15:48
answered Nov 13 '18 at 15:01
eag845eag845
878611
878611
Thank you so much, the one I was expecting. :-)
– PremKumar
Nov 13 '18 at 15:21
1
I've updated the code.
– eag845
Nov 13 '18 at 15:50
Great, thank you :)
– PremKumar
Nov 13 '18 at 15:59
add a comment |
Thank you so much, the one I was expecting. :-)
– PremKumar
Nov 13 '18 at 15:21
1
I've updated the code.
– eag845
Nov 13 '18 at 15:50
Great, thank you :)
– PremKumar
Nov 13 '18 at 15:59
Thank you so much, the one I was expecting. :-)
– PremKumar
Nov 13 '18 at 15:21
Thank you so much, the one I was expecting. :-)
– PremKumar
Nov 13 '18 at 15:21
1
1
I've updated the code.
– eag845
Nov 13 '18 at 15:50
I've updated the code.
– eag845
Nov 13 '18 at 15:50
Great, thank you :)
– PremKumar
Nov 13 '18 at 15:59
Great, thank you :)
– PremKumar
Nov 13 '18 at 15:59
add a comment |
You can create the function like:
function ageCategory(age){
output.innerHTML =''
employees.forEach(function(employee){
const ageFilter = getAge(employee.dob)
if(age == 25 && ageFilter < 25 ){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
else if(age == 50 && ageFilter >= 25 && ageFilter <= 50){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
else if(age == 100 && ageFilter >= 50 && ageFilter <= 100){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
}
}
Then use the function as call back of addEventListener()
by passing the age like the following way:
document.getElementById('btn-25').addEventListener('click', ageCategory(25));
document.getElementById('btn-50').addEventListener('click', ageCategory(50));
document.getElementById('btn-100').addEventListener('click', ageCategory(100));
add a comment |
You can create the function like:
function ageCategory(age){
output.innerHTML =''
employees.forEach(function(employee){
const ageFilter = getAge(employee.dob)
if(age == 25 && ageFilter < 25 ){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
else if(age == 50 && ageFilter >= 25 && ageFilter <= 50){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
else if(age == 100 && ageFilter >= 50 && ageFilter <= 100){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
}
}
Then use the function as call back of addEventListener()
by passing the age like the following way:
document.getElementById('btn-25').addEventListener('click', ageCategory(25));
document.getElementById('btn-50').addEventListener('click', ageCategory(50));
document.getElementById('btn-100').addEventListener('click', ageCategory(100));
add a comment |
You can create the function like:
function ageCategory(age){
output.innerHTML =''
employees.forEach(function(employee){
const ageFilter = getAge(employee.dob)
if(age == 25 && ageFilter < 25 ){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
else if(age == 50 && ageFilter >= 25 && ageFilter <= 50){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
else if(age == 100 && ageFilter >= 50 && ageFilter <= 100){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
}
}
Then use the function as call back of addEventListener()
by passing the age like the following way:
document.getElementById('btn-25').addEventListener('click', ageCategory(25));
document.getElementById('btn-50').addEventListener('click', ageCategory(50));
document.getElementById('btn-100').addEventListener('click', ageCategory(100));
You can create the function like:
function ageCategory(age){
output.innerHTML =''
employees.forEach(function(employee){
const ageFilter = getAge(employee.dob)
if(age == 25 && ageFilter < 25 ){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
else if(age == 50 && ageFilter >= 25 && ageFilter <= 50){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
else if(age == 100 && ageFilter >= 50 && ageFilter <= 100){
output.innerHTML += `<h3> ${employee.name} </h3>`
}
}
}
Then use the function as call back of addEventListener()
by passing the age like the following way:
document.getElementById('btn-25').addEventListener('click', ageCategory(25));
document.getElementById('btn-50').addEventListener('click', ageCategory(50));
document.getElementById('btn-100').addEventListener('click', ageCategory(100));
edited Nov 13 '18 at 16:06
answered Nov 13 '18 at 14:59
MamunMamun
26.6k71630
26.6k71630
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283735%2fdeclaring-foreach-globally-and-using-it-in-functions%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
yes, you could have one function called from all your buttons, if you just tell it the thing it needs to do differently each time. In this case it seems the parameters of the if statement appear to be the only thing which varies, so those values would be the things to use as the input parameters for your single function. (P.S. the ability to abstract your code like this and pick out the things which are variable and the things which are the same each time is a key skill if you want to be able to write lean, non-repetitive code).
– ADyson
Nov 13 '18 at 14:58
@ADyson Thank you so much for your valuable note :)
– PremKumar
Nov 13 '18 at 15:24