The following Node module returns “undefined”, but why?












0















Below is a module that takes one argument, a number, and then either adds or subtracts a const, magicNumber, from it depending on whether or not the number is even or odd, respectively. When I run this code, however, I simply get "undefined." What am I doing wrong?



module.exports = (number) => {

let answer; //Answer is declared in the global scope
const magicNumber = 5; //magicNumber is declared in the global scope

if (number % 2) { //If the number is even
add(number); //run the number through the add function
} else { //otherwise run the number through the subtract function
subtract(number);
}

function add(number){ //Function takes the number as argument, does the math, and returns the value of answer.
answer = number + magicNumber;
return answer;
}

function subtract(number){ //Function takes the number as argument, does the math, and returns the value of answer.
answer = number - magicNumber;
return answer;
}
};









share|improve this question




















  • 3





    Your function isn't returning anything, so the return value is undefined by default (also, comments need to be delimited with /* / */ or, on a single line, with // (* alone won't work)

    – CertainPerformance
    Nov 23 '18 at 1:58













  • You need to return something from your function, like return add(number); and return subtract(number)

    – Paulpro
    Nov 23 '18 at 2:54













  • It is difficult to understand if the code not shown as code is intentional or not (e.g. the first line "module.exports..." and the final line "};" are not displaying as code in your question. Your question should also include the code you're using to call the module.

    – bob
    Nov 23 '18 at 2:56
















0















Below is a module that takes one argument, a number, and then either adds or subtracts a const, magicNumber, from it depending on whether or not the number is even or odd, respectively. When I run this code, however, I simply get "undefined." What am I doing wrong?



module.exports = (number) => {

let answer; //Answer is declared in the global scope
const magicNumber = 5; //magicNumber is declared in the global scope

if (number % 2) { //If the number is even
add(number); //run the number through the add function
} else { //otherwise run the number through the subtract function
subtract(number);
}

function add(number){ //Function takes the number as argument, does the math, and returns the value of answer.
answer = number + magicNumber;
return answer;
}

function subtract(number){ //Function takes the number as argument, does the math, and returns the value of answer.
answer = number - magicNumber;
return answer;
}
};









share|improve this question




















  • 3





    Your function isn't returning anything, so the return value is undefined by default (also, comments need to be delimited with /* / */ or, on a single line, with // (* alone won't work)

    – CertainPerformance
    Nov 23 '18 at 1:58













  • You need to return something from your function, like return add(number); and return subtract(number)

    – Paulpro
    Nov 23 '18 at 2:54













  • It is difficult to understand if the code not shown as code is intentional or not (e.g. the first line "module.exports..." and the final line "};" are not displaying as code in your question. Your question should also include the code you're using to call the module.

    – bob
    Nov 23 '18 at 2:56














0












0








0








Below is a module that takes one argument, a number, and then either adds or subtracts a const, magicNumber, from it depending on whether or not the number is even or odd, respectively. When I run this code, however, I simply get "undefined." What am I doing wrong?



module.exports = (number) => {

let answer; //Answer is declared in the global scope
const magicNumber = 5; //magicNumber is declared in the global scope

if (number % 2) { //If the number is even
add(number); //run the number through the add function
} else { //otherwise run the number through the subtract function
subtract(number);
}

function add(number){ //Function takes the number as argument, does the math, and returns the value of answer.
answer = number + magicNumber;
return answer;
}

function subtract(number){ //Function takes the number as argument, does the math, and returns the value of answer.
answer = number - magicNumber;
return answer;
}
};









share|improve this question
















Below is a module that takes one argument, a number, and then either adds or subtracts a const, magicNumber, from it depending on whether or not the number is even or odd, respectively. When I run this code, however, I simply get "undefined." What am I doing wrong?



module.exports = (number) => {

let answer; //Answer is declared in the global scope
const magicNumber = 5; //magicNumber is declared in the global scope

if (number % 2) { //If the number is even
add(number); //run the number through the add function
} else { //otherwise run the number through the subtract function
subtract(number);
}

function add(number){ //Function takes the number as argument, does the math, and returns the value of answer.
answer = number + magicNumber;
return answer;
}

function subtract(number){ //Function takes the number as argument, does the math, and returns the value of answer.
answer = number - magicNumber;
return answer;
}
};






javascript return return-value node-modules






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 6:20









Abhishek Soni

1,19911232




1,19911232










asked Nov 23 '18 at 1:57









user10693340user10693340

112




112








  • 3





    Your function isn't returning anything, so the return value is undefined by default (also, comments need to be delimited with /* / */ or, on a single line, with // (* alone won't work)

    – CertainPerformance
    Nov 23 '18 at 1:58













  • You need to return something from your function, like return add(number); and return subtract(number)

    – Paulpro
    Nov 23 '18 at 2:54













  • It is difficult to understand if the code not shown as code is intentional or not (e.g. the first line "module.exports..." and the final line "};" are not displaying as code in your question. Your question should also include the code you're using to call the module.

    – bob
    Nov 23 '18 at 2:56














  • 3





    Your function isn't returning anything, so the return value is undefined by default (also, comments need to be delimited with /* / */ or, on a single line, with // (* alone won't work)

    – CertainPerformance
    Nov 23 '18 at 1:58













  • You need to return something from your function, like return add(number); and return subtract(number)

    – Paulpro
    Nov 23 '18 at 2:54













  • It is difficult to understand if the code not shown as code is intentional or not (e.g. the first line "module.exports..." and the final line "};" are not displaying as code in your question. Your question should also include the code you're using to call the module.

    – bob
    Nov 23 '18 at 2:56








3




3





Your function isn't returning anything, so the return value is undefined by default (also, comments need to be delimited with /* / */ or, on a single line, with // (* alone won't work)

– CertainPerformance
Nov 23 '18 at 1:58







Your function isn't returning anything, so the return value is undefined by default (also, comments need to be delimited with /* / */ or, on a single line, with // (* alone won't work)

– CertainPerformance
Nov 23 '18 at 1:58















You need to return something from your function, like return add(number); and return subtract(number)

– Paulpro
Nov 23 '18 at 2:54







You need to return something from your function, like return add(number); and return subtract(number)

– Paulpro
Nov 23 '18 at 2:54















It is difficult to understand if the code not shown as code is intentional or not (e.g. the first line "module.exports..." and the final line "};" are not displaying as code in your question. Your question should also include the code you're using to call the module.

– bob
Nov 23 '18 at 2:56





It is difficult to understand if the code not shown as code is intentional or not (e.g. the first line "module.exports..." and the final line "};" are not displaying as code in your question. Your question should also include the code you're using to call the module.

– bob
Nov 23 '18 at 2:56












1 Answer
1






active

oldest

votes


















2














Your exported block is not returning anything hence it’s undefined by default.



module.exports = (number) => {
let answer; //Answer is declared in the global scope
const magicNumber = 5; //magicNumber is declared in the global scope

if (number % 2) { //If the number is even
return add(number); //you should return here as well
} else { //otherwise run the number through the subtract function
return subtract(number);//you should return here as well
}

function add(number){ //Function takes the number as argument, does the math, and returns the value of answer.
answer = number + magicNumber;
return answer;
}

function subtract(number){ //Function takes the number as argument, does the math, and returns the value of answer.
answer = number - magicNumber;
return answer;
}
};





share|improve this answer
























  • My logic was that since one or the other function, add() or subtract(), respectively, would inevitably be called and each of those have their own returns, the intended outcome of the code would inevitably be returned. It's confusing to me that return statements would have to drive the continual execution of the code at every step when it seems intuitive that once either of the lines add(number); or subtract(number); are hit, those functions would run and would return values regardless. I'm currently researching this but finding nothing. Is there something fundamental I'm misunderstanding?

    – user10693340
    Nov 23 '18 at 15:55











  • In JavaScript and almost all programming languages return statement is block based

    – Ravinder Payal
    Nov 23 '18 at 15:59











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


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439818%2fthe-following-node-module-returns-undefined-but-why%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Your exported block is not returning anything hence it’s undefined by default.



module.exports = (number) => {
let answer; //Answer is declared in the global scope
const magicNumber = 5; //magicNumber is declared in the global scope

if (number % 2) { //If the number is even
return add(number); //you should return here as well
} else { //otherwise run the number through the subtract function
return subtract(number);//you should return here as well
}

function add(number){ //Function takes the number as argument, does the math, and returns the value of answer.
answer = number + magicNumber;
return answer;
}

function subtract(number){ //Function takes the number as argument, does the math, and returns the value of answer.
answer = number - magicNumber;
return answer;
}
};





share|improve this answer
























  • My logic was that since one or the other function, add() or subtract(), respectively, would inevitably be called and each of those have their own returns, the intended outcome of the code would inevitably be returned. It's confusing to me that return statements would have to drive the continual execution of the code at every step when it seems intuitive that once either of the lines add(number); or subtract(number); are hit, those functions would run and would return values regardless. I'm currently researching this but finding nothing. Is there something fundamental I'm misunderstanding?

    – user10693340
    Nov 23 '18 at 15:55











  • In JavaScript and almost all programming languages return statement is block based

    – Ravinder Payal
    Nov 23 '18 at 15:59
















2














Your exported block is not returning anything hence it’s undefined by default.



module.exports = (number) => {
let answer; //Answer is declared in the global scope
const magicNumber = 5; //magicNumber is declared in the global scope

if (number % 2) { //If the number is even
return add(number); //you should return here as well
} else { //otherwise run the number through the subtract function
return subtract(number);//you should return here as well
}

function add(number){ //Function takes the number as argument, does the math, and returns the value of answer.
answer = number + magicNumber;
return answer;
}

function subtract(number){ //Function takes the number as argument, does the math, and returns the value of answer.
answer = number - magicNumber;
return answer;
}
};





share|improve this answer
























  • My logic was that since one or the other function, add() or subtract(), respectively, would inevitably be called and each of those have their own returns, the intended outcome of the code would inevitably be returned. It's confusing to me that return statements would have to drive the continual execution of the code at every step when it seems intuitive that once either of the lines add(number); or subtract(number); are hit, those functions would run and would return values regardless. I'm currently researching this but finding nothing. Is there something fundamental I'm misunderstanding?

    – user10693340
    Nov 23 '18 at 15:55











  • In JavaScript and almost all programming languages return statement is block based

    – Ravinder Payal
    Nov 23 '18 at 15:59














2












2








2







Your exported block is not returning anything hence it’s undefined by default.



module.exports = (number) => {
let answer; //Answer is declared in the global scope
const magicNumber = 5; //magicNumber is declared in the global scope

if (number % 2) { //If the number is even
return add(number); //you should return here as well
} else { //otherwise run the number through the subtract function
return subtract(number);//you should return here as well
}

function add(number){ //Function takes the number as argument, does the math, and returns the value of answer.
answer = number + magicNumber;
return answer;
}

function subtract(number){ //Function takes the number as argument, does the math, and returns the value of answer.
answer = number - magicNumber;
return answer;
}
};





share|improve this answer













Your exported block is not returning anything hence it’s undefined by default.



module.exports = (number) => {
let answer; //Answer is declared in the global scope
const magicNumber = 5; //magicNumber is declared in the global scope

if (number % 2) { //If the number is even
return add(number); //you should return here as well
} else { //otherwise run the number through the subtract function
return subtract(number);//you should return here as well
}

function add(number){ //Function takes the number as argument, does the math, and returns the value of answer.
answer = number + magicNumber;
return answer;
}

function subtract(number){ //Function takes the number as argument, does the math, and returns the value of answer.
answer = number - magicNumber;
return answer;
}
};






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 3:37









Ravinder PayalRavinder Payal

1,4541328




1,4541328













  • My logic was that since one or the other function, add() or subtract(), respectively, would inevitably be called and each of those have their own returns, the intended outcome of the code would inevitably be returned. It's confusing to me that return statements would have to drive the continual execution of the code at every step when it seems intuitive that once either of the lines add(number); or subtract(number); are hit, those functions would run and would return values regardless. I'm currently researching this but finding nothing. Is there something fundamental I'm misunderstanding?

    – user10693340
    Nov 23 '18 at 15:55











  • In JavaScript and almost all programming languages return statement is block based

    – Ravinder Payal
    Nov 23 '18 at 15:59



















  • My logic was that since one or the other function, add() or subtract(), respectively, would inevitably be called and each of those have their own returns, the intended outcome of the code would inevitably be returned. It's confusing to me that return statements would have to drive the continual execution of the code at every step when it seems intuitive that once either of the lines add(number); or subtract(number); are hit, those functions would run and would return values regardless. I'm currently researching this but finding nothing. Is there something fundamental I'm misunderstanding?

    – user10693340
    Nov 23 '18 at 15:55











  • In JavaScript and almost all programming languages return statement is block based

    – Ravinder Payal
    Nov 23 '18 at 15:59

















My logic was that since one or the other function, add() or subtract(), respectively, would inevitably be called and each of those have their own returns, the intended outcome of the code would inevitably be returned. It's confusing to me that return statements would have to drive the continual execution of the code at every step when it seems intuitive that once either of the lines add(number); or subtract(number); are hit, those functions would run and would return values regardless. I'm currently researching this but finding nothing. Is there something fundamental I'm misunderstanding?

– user10693340
Nov 23 '18 at 15:55





My logic was that since one or the other function, add() or subtract(), respectively, would inevitably be called and each of those have their own returns, the intended outcome of the code would inevitably be returned. It's confusing to me that return statements would have to drive the continual execution of the code at every step when it seems intuitive that once either of the lines add(number); or subtract(number); are hit, those functions would run and would return values regardless. I'm currently researching this but finding nothing. Is there something fundamental I'm misunderstanding?

– user10693340
Nov 23 '18 at 15:55













In JavaScript and almost all programming languages return statement is block based

– Ravinder Payal
Nov 23 '18 at 15:59





In JavaScript and almost all programming languages return statement is block based

– Ravinder Payal
Nov 23 '18 at 15:59




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439818%2fthe-following-node-module-returns-undefined-but-why%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?