Call a function from an if-statement inside a function
I have a function with an if-statement that in turn should call an outside function whether or not something is true. The result I get is 'undefined'. Could you please point me in the right direction?
Function 1:
function printRange(rangeStart, rangeStop)
let summa = ;
for (i = rangeStart; i <= rangeStop; i++)
summa.push(i);
let result2 = summa.join();
return result2;
Function 2:
function printRangeReversed(rangeStart, rangeStop)
let summa = ;
for (i = rangeStart; i >= rangeStop; i--)
summa.push(i);
let result3 = summa.join();
return result3;
Function 3:
function printAnyRange(rangeStart, rangeStop)
if (rangeStart < rangeStop)
printRange(rangeStart, rangeStop);
else
printRangeReversed(rangeStart, rangeStop);
return;
Calling the function: printAnyRange(21, 45);
As I said it all looks logical to me but I guess it isn't since I get the 'undefined'.
Expected result should be: "21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45"
Regards.
javascript function if-statement
add a comment |
I have a function with an if-statement that in turn should call an outside function whether or not something is true. The result I get is 'undefined'. Could you please point me in the right direction?
Function 1:
function printRange(rangeStart, rangeStop)
let summa = ;
for (i = rangeStart; i <= rangeStop; i++)
summa.push(i);
let result2 = summa.join();
return result2;
Function 2:
function printRangeReversed(rangeStart, rangeStop)
let summa = ;
for (i = rangeStart; i >= rangeStop; i--)
summa.push(i);
let result3 = summa.join();
return result3;
Function 3:
function printAnyRange(rangeStart, rangeStop)
if (rangeStart < rangeStop)
printRange(rangeStart, rangeStop);
else
printRangeReversed(rangeStart, rangeStop);
return;
Calling the function: printAnyRange(21, 45);
As I said it all looks logical to me but I guess it isn't since I get the 'undefined'.
Expected result should be: "21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45"
Regards.
javascript function if-statement
Yourreturn
statement is not followed by an expression, so the function returnsundefined
. Your other functions return values, but when your main function calls them it does not save the values returned.
– Pointy
Nov 16 '18 at 0:48
a) you don't return anything fromprintAnyRange
and b) you dont' even "capture" the values returned from the other functions ... so, no surprise you get undefined at all
– Bravo
Nov 16 '18 at 0:49
"I have a function with an if-statement that in turn should call an outside function whether or not something is true. The result I get is 'undefined'." All of this is correct and as expected. What is your question?
– melpomene
Nov 16 '18 at 0:51
@melpomene Sorry, added the expected result now.
– Thomas Bengtsson
Nov 16 '18 at 0:55
add a comment |
I have a function with an if-statement that in turn should call an outside function whether or not something is true. The result I get is 'undefined'. Could you please point me in the right direction?
Function 1:
function printRange(rangeStart, rangeStop)
let summa = ;
for (i = rangeStart; i <= rangeStop; i++)
summa.push(i);
let result2 = summa.join();
return result2;
Function 2:
function printRangeReversed(rangeStart, rangeStop)
let summa = ;
for (i = rangeStart; i >= rangeStop; i--)
summa.push(i);
let result3 = summa.join();
return result3;
Function 3:
function printAnyRange(rangeStart, rangeStop)
if (rangeStart < rangeStop)
printRange(rangeStart, rangeStop);
else
printRangeReversed(rangeStart, rangeStop);
return;
Calling the function: printAnyRange(21, 45);
As I said it all looks logical to me but I guess it isn't since I get the 'undefined'.
Expected result should be: "21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45"
Regards.
javascript function if-statement
I have a function with an if-statement that in turn should call an outside function whether or not something is true. The result I get is 'undefined'. Could you please point me in the right direction?
Function 1:
function printRange(rangeStart, rangeStop)
let summa = ;
for (i = rangeStart; i <= rangeStop; i++)
summa.push(i);
let result2 = summa.join();
return result2;
Function 2:
function printRangeReversed(rangeStart, rangeStop)
let summa = ;
for (i = rangeStart; i >= rangeStop; i--)
summa.push(i);
let result3 = summa.join();
return result3;
Function 3:
function printAnyRange(rangeStart, rangeStop)
if (rangeStart < rangeStop)
printRange(rangeStart, rangeStop);
else
printRangeReversed(rangeStart, rangeStop);
return;
Calling the function: printAnyRange(21, 45);
As I said it all looks logical to me but I guess it isn't since I get the 'undefined'.
Expected result should be: "21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45"
Regards.
javascript function if-statement
javascript function if-statement
edited Nov 16 '18 at 4:45
Jack Bashford
11.7k31846
11.7k31846
asked Nov 16 '18 at 0:46
Thomas BengtssonThomas Bengtsson
18613
18613
Yourreturn
statement is not followed by an expression, so the function returnsundefined
. Your other functions return values, but when your main function calls them it does not save the values returned.
– Pointy
Nov 16 '18 at 0:48
a) you don't return anything fromprintAnyRange
and b) you dont' even "capture" the values returned from the other functions ... so, no surprise you get undefined at all
– Bravo
Nov 16 '18 at 0:49
"I have a function with an if-statement that in turn should call an outside function whether or not something is true. The result I get is 'undefined'." All of this is correct and as expected. What is your question?
– melpomene
Nov 16 '18 at 0:51
@melpomene Sorry, added the expected result now.
– Thomas Bengtsson
Nov 16 '18 at 0:55
add a comment |
Yourreturn
statement is not followed by an expression, so the function returnsundefined
. Your other functions return values, but when your main function calls them it does not save the values returned.
– Pointy
Nov 16 '18 at 0:48
a) you don't return anything fromprintAnyRange
and b) you dont' even "capture" the values returned from the other functions ... so, no surprise you get undefined at all
– Bravo
Nov 16 '18 at 0:49
"I have a function with an if-statement that in turn should call an outside function whether or not something is true. The result I get is 'undefined'." All of this is correct and as expected. What is your question?
– melpomene
Nov 16 '18 at 0:51
@melpomene Sorry, added the expected result now.
– Thomas Bengtsson
Nov 16 '18 at 0:55
Your
return
statement is not followed by an expression, so the function returns undefined
. Your other functions return values, but when your main function calls them it does not save the values returned.– Pointy
Nov 16 '18 at 0:48
Your
return
statement is not followed by an expression, so the function returns undefined
. Your other functions return values, but when your main function calls them it does not save the values returned.– Pointy
Nov 16 '18 at 0:48
a) you don't return anything from
printAnyRange
and b) you dont' even "capture" the values returned from the other functions ... so, no surprise you get undefined at all– Bravo
Nov 16 '18 at 0:49
a) you don't return anything from
printAnyRange
and b) you dont' even "capture" the values returned from the other functions ... so, no surprise you get undefined at all– Bravo
Nov 16 '18 at 0:49
"I have a function with an if-statement that in turn should call an outside function whether or not something is true. The result I get is 'undefined'." All of this is correct and as expected. What is your question?
– melpomene
Nov 16 '18 at 0:51
"I have a function with an if-statement that in turn should call an outside function whether or not something is true. The result I get is 'undefined'." All of this is correct and as expected. What is your question?
– melpomene
Nov 16 '18 at 0:51
@melpomene Sorry, added the expected result now.
– Thomas Bengtsson
Nov 16 '18 at 0:55
@melpomene Sorry, added the expected result now.
– Thomas Bengtsson
Nov 16 '18 at 0:55
add a comment |
3 Answers
3
active
oldest
votes
You're not returning anything. Capture the result of the functions and return that variable.
function printRange(rangeStart, rangeStop)
let summa = ;
for (i = rangeStart; i <= rangeStop; i++)
summa.push(i);
let result2 = summa.join();
return result2;
function printRangeReversed(rangeStart, rangeStop)
let summa = ;
for (i = rangeStart; i >= rangeStop; i--)
summa.push(i);
let result3 = summa.join();
return result3;
function printAnyRange(rangeStart, rangeStop)
let result = null;
if (rangeStart < rangeStop) result = printRange(rangeStart, rangeStop);
else result = printRangeReversed(rangeStart, rangeStop);
return result;
console.log(printAnyRange(1,5));
console.log(printAnyRange(5,1));
add a comment |
The value that is getting returned from function 1
and function 2
is not being using or stored by function 3
. Try following code:
function printAnyRange(rangeStart, rangeStop)
if (rangeStart < rangeStop)
return printRange(rangeStart, rangeStop);
else
return printRangeReversed(rangeStart, rangeStop);
console.log(printAnyRange(10, 15));
this should fix the issue.
add a comment |
You need to do something with the returned values, like log them:
function printAnyRange(rangeStart, rangeStop)
if (rangeStart < rangeStop)
console.log(printRange(rangeStart, rangeStop));
else
console.log(printRangeReversed(rangeStart, rangeStop));
return;
Or return them from the main function:
function printAnyRange(rangeStart, rangeStop)
if (rangeStart < rangeStop)
result = printRange(rangeStart, rangeStop);
else
result = printRangeReversed(rangeStart, rangeStop);
return result;
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%2f53329879%2fcall-a-function-from-an-if-statement-inside-a-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You're not returning anything. Capture the result of the functions and return that variable.
function printRange(rangeStart, rangeStop)
let summa = ;
for (i = rangeStart; i <= rangeStop; i++)
summa.push(i);
let result2 = summa.join();
return result2;
function printRangeReversed(rangeStart, rangeStop)
let summa = ;
for (i = rangeStart; i >= rangeStop; i--)
summa.push(i);
let result3 = summa.join();
return result3;
function printAnyRange(rangeStart, rangeStop)
let result = null;
if (rangeStart < rangeStop) result = printRange(rangeStart, rangeStop);
else result = printRangeReversed(rangeStart, rangeStop);
return result;
console.log(printAnyRange(1,5));
console.log(printAnyRange(5,1));
add a comment |
You're not returning anything. Capture the result of the functions and return that variable.
function printRange(rangeStart, rangeStop)
let summa = ;
for (i = rangeStart; i <= rangeStop; i++)
summa.push(i);
let result2 = summa.join();
return result2;
function printRangeReversed(rangeStart, rangeStop)
let summa = ;
for (i = rangeStart; i >= rangeStop; i--)
summa.push(i);
let result3 = summa.join();
return result3;
function printAnyRange(rangeStart, rangeStop)
let result = null;
if (rangeStart < rangeStop) result = printRange(rangeStart, rangeStop);
else result = printRangeReversed(rangeStart, rangeStop);
return result;
console.log(printAnyRange(1,5));
console.log(printAnyRange(5,1));
add a comment |
You're not returning anything. Capture the result of the functions and return that variable.
function printRange(rangeStart, rangeStop)
let summa = ;
for (i = rangeStart; i <= rangeStop; i++)
summa.push(i);
let result2 = summa.join();
return result2;
function printRangeReversed(rangeStart, rangeStop)
let summa = ;
for (i = rangeStart; i >= rangeStop; i--)
summa.push(i);
let result3 = summa.join();
return result3;
function printAnyRange(rangeStart, rangeStop)
let result = null;
if (rangeStart < rangeStop) result = printRange(rangeStart, rangeStop);
else result = printRangeReversed(rangeStart, rangeStop);
return result;
console.log(printAnyRange(1,5));
console.log(printAnyRange(5,1));
You're not returning anything. Capture the result of the functions and return that variable.
function printRange(rangeStart, rangeStop)
let summa = ;
for (i = rangeStart; i <= rangeStop; i++)
summa.push(i);
let result2 = summa.join();
return result2;
function printRangeReversed(rangeStart, rangeStop)
let summa = ;
for (i = rangeStart; i >= rangeStop; i--)
summa.push(i);
let result3 = summa.join();
return result3;
function printAnyRange(rangeStart, rangeStop)
let result = null;
if (rangeStart < rangeStop) result = printRange(rangeStart, rangeStop);
else result = printRangeReversed(rangeStart, rangeStop);
return result;
console.log(printAnyRange(1,5));
console.log(printAnyRange(5,1));
function printRange(rangeStart, rangeStop)
let summa = ;
for (i = rangeStart; i <= rangeStop; i++)
summa.push(i);
let result2 = summa.join();
return result2;
function printRangeReversed(rangeStart, rangeStop)
let summa = ;
for (i = rangeStart; i >= rangeStop; i--)
summa.push(i);
let result3 = summa.join();
return result3;
function printAnyRange(rangeStart, rangeStop)
let result = null;
if (rangeStart < rangeStop) result = printRange(rangeStart, rangeStop);
else result = printRangeReversed(rangeStart, rangeStop);
return result;
console.log(printAnyRange(1,5));
console.log(printAnyRange(5,1));
function printRange(rangeStart, rangeStop)
let summa = ;
for (i = rangeStart; i <= rangeStop; i++)
summa.push(i);
let result2 = summa.join();
return result2;
function printRangeReversed(rangeStart, rangeStop)
let summa = ;
for (i = rangeStart; i >= rangeStop; i--)
summa.push(i);
let result3 = summa.join();
return result3;
function printAnyRange(rangeStart, rangeStop)
let result = null;
if (rangeStart < rangeStop) result = printRange(rangeStart, rangeStop);
else result = printRangeReversed(rangeStart, rangeStop);
return result;
console.log(printAnyRange(1,5));
console.log(printAnyRange(5,1));
answered Nov 16 '18 at 0:51
Abana ClaraAbana Clara
1,666920
1,666920
add a comment |
add a comment |
The value that is getting returned from function 1
and function 2
is not being using or stored by function 3
. Try following code:
function printAnyRange(rangeStart, rangeStop)
if (rangeStart < rangeStop)
return printRange(rangeStart, rangeStop);
else
return printRangeReversed(rangeStart, rangeStop);
console.log(printAnyRange(10, 15));
this should fix the issue.
add a comment |
The value that is getting returned from function 1
and function 2
is not being using or stored by function 3
. Try following code:
function printAnyRange(rangeStart, rangeStop)
if (rangeStart < rangeStop)
return printRange(rangeStart, rangeStop);
else
return printRangeReversed(rangeStart, rangeStop);
console.log(printAnyRange(10, 15));
this should fix the issue.
add a comment |
The value that is getting returned from function 1
and function 2
is not being using or stored by function 3
. Try following code:
function printAnyRange(rangeStart, rangeStop)
if (rangeStart < rangeStop)
return printRange(rangeStart, rangeStop);
else
return printRangeReversed(rangeStart, rangeStop);
console.log(printAnyRange(10, 15));
this should fix the issue.
The value that is getting returned from function 1
and function 2
is not being using or stored by function 3
. Try following code:
function printAnyRange(rangeStart, rangeStop)
if (rangeStart < rangeStop)
return printRange(rangeStart, rangeStop);
else
return printRangeReversed(rangeStart, rangeStop);
console.log(printAnyRange(10, 15));
this should fix the issue.
answered Nov 16 '18 at 1:01
Pranay TripathiPranay Tripathi
542513
542513
add a comment |
add a comment |
You need to do something with the returned values, like log them:
function printAnyRange(rangeStart, rangeStop)
if (rangeStart < rangeStop)
console.log(printRange(rangeStart, rangeStop));
else
console.log(printRangeReversed(rangeStart, rangeStop));
return;
Or return them from the main function:
function printAnyRange(rangeStart, rangeStop)
if (rangeStart < rangeStop)
result = printRange(rangeStart, rangeStop);
else
result = printRangeReversed(rangeStart, rangeStop);
return result;
add a comment |
You need to do something with the returned values, like log them:
function printAnyRange(rangeStart, rangeStop)
if (rangeStart < rangeStop)
console.log(printRange(rangeStart, rangeStop));
else
console.log(printRangeReversed(rangeStart, rangeStop));
return;
Or return them from the main function:
function printAnyRange(rangeStart, rangeStop)
if (rangeStart < rangeStop)
result = printRange(rangeStart, rangeStop);
else
result = printRangeReversed(rangeStart, rangeStop);
return result;
add a comment |
You need to do something with the returned values, like log them:
function printAnyRange(rangeStart, rangeStop)
if (rangeStart < rangeStop)
console.log(printRange(rangeStart, rangeStop));
else
console.log(printRangeReversed(rangeStart, rangeStop));
return;
Or return them from the main function:
function printAnyRange(rangeStart, rangeStop)
if (rangeStart < rangeStop)
result = printRange(rangeStart, rangeStop);
else
result = printRangeReversed(rangeStart, rangeStop);
return result;
You need to do something with the returned values, like log them:
function printAnyRange(rangeStart, rangeStop)
if (rangeStart < rangeStop)
console.log(printRange(rangeStart, rangeStop));
else
console.log(printRangeReversed(rangeStart, rangeStop));
return;
Or return them from the main function:
function printAnyRange(rangeStart, rangeStop)
if (rangeStart < rangeStop)
result = printRange(rangeStart, rangeStop);
else
result = printRangeReversed(rangeStart, rangeStop);
return result;
answered Nov 16 '18 at 0:51
Jack BashfordJack Bashford
11.7k31846
11.7k31846
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%2f53329879%2fcall-a-function-from-an-if-statement-inside-a-function%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
Your
return
statement is not followed by an expression, so the function returnsundefined
. Your other functions return values, but when your main function calls them it does not save the values returned.– Pointy
Nov 16 '18 at 0:48
a) you don't return anything from
printAnyRange
and b) you dont' even "capture" the values returned from the other functions ... so, no surprise you get undefined at all– Bravo
Nov 16 '18 at 0:49
"I have a function with an if-statement that in turn should call an outside function whether or not something is true. The result I get is 'undefined'." All of this is correct and as expected. What is your question?
– melpomene
Nov 16 '18 at 0:51
@melpomene Sorry, added the expected result now.
– Thomas Bengtsson
Nov 16 '18 at 0:55