Much frustrated gnashing from undefined function return type
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
My experience with recursive calls is not deep or long, but I thought this problem would make for a good evaluation. Most of it appears to work, but the function returns undefined:
const target = 1234; // count the number of digits
function numDigits(num, count=0){
let div10 = count;
div10 += 1;
if(Math.floor(num / 10) < 1){
console.log(`Entered the end and div10 is ${div10}`);
return div10; // returning undefined
}
let curr = Math.floor(num / 10);
console.log(`Bottom of call and dividing ${curr}`);
numDigits(curr, div10);
}
numDigits(target);
javascript recursion undefined
add a comment |
My experience with recursive calls is not deep or long, but I thought this problem would make for a good evaluation. Most of it appears to work, but the function returns undefined:
const target = 1234; // count the number of digits
function numDigits(num, count=0){
let div10 = count;
div10 += 1;
if(Math.floor(num / 10) < 1){
console.log(`Entered the end and div10 is ${div10}`);
return div10; // returning undefined
}
let curr = Math.floor(num / 10);
console.log(`Bottom of call and dividing ${curr}`);
numDigits(curr, div10);
}
numDigits(target);
javascript recursion undefined
add a comment |
My experience with recursive calls is not deep or long, but I thought this problem would make for a good evaluation. Most of it appears to work, but the function returns undefined:
const target = 1234; // count the number of digits
function numDigits(num, count=0){
let div10 = count;
div10 += 1;
if(Math.floor(num / 10) < 1){
console.log(`Entered the end and div10 is ${div10}`);
return div10; // returning undefined
}
let curr = Math.floor(num / 10);
console.log(`Bottom of call and dividing ${curr}`);
numDigits(curr, div10);
}
numDigits(target);
javascript recursion undefined
My experience with recursive calls is not deep or long, but I thought this problem would make for a good evaluation. Most of it appears to work, but the function returns undefined:
const target = 1234; // count the number of digits
function numDigits(num, count=0){
let div10 = count;
div10 += 1;
if(Math.floor(num / 10) < 1){
console.log(`Entered the end and div10 is ${div10}`);
return div10; // returning undefined
}
let curr = Math.floor(num / 10);
console.log(`Bottom of call and dividing ${curr}`);
numDigits(curr, div10);
}
numDigits(target);
javascript recursion undefined
javascript recursion undefined
asked Jan 3 at 3:25
jobechoijobechoi
295
295
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You miss a return
when calling recursively numDigits
:
const target = 1234; // count the number of digits
function numDigits(num, count=0){
let div10 = count;
div10 += 1;
if(Math.floor(num / 10) < 1){
console.log(`Entered the end and div10 is ${div10}`);
return div10; // returning undefined
}
let curr = Math.floor(num / 10);
console.log(`Bottom of call and dividing ${curr}`);
return numDigits(curr, div10); // HERE
}
console.log(numDigits(target));
Thanks! Not sure I understand what is happening, as up until the 3rd recursive call, things appeared to work, and that was without the return. Can you elaborate on why that made it work?
– jobechoi
Jan 3 at 3:34
calls heppen, they do. But if you don't executereturn
as I added, then your func will not return anything. Does it make sense now?
– quirimmo
Jan 3 at 3:35
return isn't called (it's not a function), it's executed. ;-)
– RobG
Jan 3 at 3:38
@RobG fair enough :D thanks, edited :)
– quirimmo
Jan 3 at 3:38
I sort of get it: the recursive call at some point has to return something. In my initial code, that wasn't happening. Thanks.
– jobechoi
Jan 3 at 3:42
|
show 1 more comment
Recursion is a functional heritage and so using it with functional style yields the best results. There's no "missing return
"; there's little need for return
in functional style as it is a side effect, it does not produce a value, it cannot be called like a function, and it cannot be combined with other expressions. Other imperative style statements like the reassignment/mutation div10 += 1
are also a recipe for a migraine when used recursively.
Your program can be dramatically simplified –
const numDigits = n =>
n < 10
? 1
: 1 + numDigits (Math .floor (n / 10))
console .log
( numDigits (1) // 1
, numDigits (22) // 2
, numDigits (333) // 3
, numDigits (999999999) // 9
)
The count
variable can still be used if you prefer. This would be a practical step toward making the program stack-safe –
const numDigits = (n, count = 1) =>
n < 10
? count
: numDigits
( Math .floor (n / 10)
, count + 1
)
console .log
( numDigits (1) // 1
, numDigits (22) // 2
, numDigits (333) // 3
, numDigits (999999999) // 9
)
"There's no "missing return"" might be a reasonable statement if you hadn't added an implicit return through use of an arrow function. There is indeed a missing return statement, your refactored code is effectivelyif (n < 10) {return count} else {return numDigits(Math.floor (n / 10), count + 1)}
. Removing the redundant else but keeping the return statement in the block is effectively identical to the OP's code. QED.
– RobG
Jan 3 at 8:52
The ternary is simple and succinct. As stated: new to recursion. I will keep this model in mind now. I see that my departure happened much earlier, even before the code was written: I never thought to check whether the integer was less than 10. Thanks, user633183.
– jobechoi
Jan 3 at 11:25
@RobG the difference lies between statements and expressions – statements likeif
,else
,foreach
,switch
,while
, andreturn
are side effects, do not produce a value, and cannot be composed with other statements. Worse,if
allows you to write conditionals without anelse
branch – ternary expressions using?:
evaluate to a value, can be composed with other expressions, and cannot be written without an "else" branch. It looks like I never got around to finishing this answer but I think you will find it useful.
– user633183
Jan 3 at 21:47
@RobG and for more about discussion about statements vs. expressions, I wish to share this. Any further discussion is welcomed and encouraged. The topics here are among my favourite :D
– user633183
Jan 3 at 22:11
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%2f54015936%2fmuch-frustrated-gnashing-from-undefined-function-return-type%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 miss a return
when calling recursively numDigits
:
const target = 1234; // count the number of digits
function numDigits(num, count=0){
let div10 = count;
div10 += 1;
if(Math.floor(num / 10) < 1){
console.log(`Entered the end and div10 is ${div10}`);
return div10; // returning undefined
}
let curr = Math.floor(num / 10);
console.log(`Bottom of call and dividing ${curr}`);
return numDigits(curr, div10); // HERE
}
console.log(numDigits(target));
Thanks! Not sure I understand what is happening, as up until the 3rd recursive call, things appeared to work, and that was without the return. Can you elaborate on why that made it work?
– jobechoi
Jan 3 at 3:34
calls heppen, they do. But if you don't executereturn
as I added, then your func will not return anything. Does it make sense now?
– quirimmo
Jan 3 at 3:35
return isn't called (it's not a function), it's executed. ;-)
– RobG
Jan 3 at 3:38
@RobG fair enough :D thanks, edited :)
– quirimmo
Jan 3 at 3:38
I sort of get it: the recursive call at some point has to return something. In my initial code, that wasn't happening. Thanks.
– jobechoi
Jan 3 at 3:42
|
show 1 more comment
You miss a return
when calling recursively numDigits
:
const target = 1234; // count the number of digits
function numDigits(num, count=0){
let div10 = count;
div10 += 1;
if(Math.floor(num / 10) < 1){
console.log(`Entered the end and div10 is ${div10}`);
return div10; // returning undefined
}
let curr = Math.floor(num / 10);
console.log(`Bottom of call and dividing ${curr}`);
return numDigits(curr, div10); // HERE
}
console.log(numDigits(target));
Thanks! Not sure I understand what is happening, as up until the 3rd recursive call, things appeared to work, and that was without the return. Can you elaborate on why that made it work?
– jobechoi
Jan 3 at 3:34
calls heppen, they do. But if you don't executereturn
as I added, then your func will not return anything. Does it make sense now?
– quirimmo
Jan 3 at 3:35
return isn't called (it's not a function), it's executed. ;-)
– RobG
Jan 3 at 3:38
@RobG fair enough :D thanks, edited :)
– quirimmo
Jan 3 at 3:38
I sort of get it: the recursive call at some point has to return something. In my initial code, that wasn't happening. Thanks.
– jobechoi
Jan 3 at 3:42
|
show 1 more comment
You miss a return
when calling recursively numDigits
:
const target = 1234; // count the number of digits
function numDigits(num, count=0){
let div10 = count;
div10 += 1;
if(Math.floor(num / 10) < 1){
console.log(`Entered the end and div10 is ${div10}`);
return div10; // returning undefined
}
let curr = Math.floor(num / 10);
console.log(`Bottom of call and dividing ${curr}`);
return numDigits(curr, div10); // HERE
}
console.log(numDigits(target));
You miss a return
when calling recursively numDigits
:
const target = 1234; // count the number of digits
function numDigits(num, count=0){
let div10 = count;
div10 += 1;
if(Math.floor(num / 10) < 1){
console.log(`Entered the end and div10 is ${div10}`);
return div10; // returning undefined
}
let curr = Math.floor(num / 10);
console.log(`Bottom of call and dividing ${curr}`);
return numDigits(curr, div10); // HERE
}
console.log(numDigits(target));
const target = 1234; // count the number of digits
function numDigits(num, count=0){
let div10 = count;
div10 += 1;
if(Math.floor(num / 10) < 1){
console.log(`Entered the end and div10 is ${div10}`);
return div10; // returning undefined
}
let curr = Math.floor(num / 10);
console.log(`Bottom of call and dividing ${curr}`);
return numDigits(curr, div10); // HERE
}
console.log(numDigits(target));
const target = 1234; // count the number of digits
function numDigits(num, count=0){
let div10 = count;
div10 += 1;
if(Math.floor(num / 10) < 1){
console.log(`Entered the end and div10 is ${div10}`);
return div10; // returning undefined
}
let curr = Math.floor(num / 10);
console.log(`Bottom of call and dividing ${curr}`);
return numDigits(curr, div10); // HERE
}
console.log(numDigits(target));
answered Jan 3 at 3:30
quirimmoquirimmo
7,72811536
7,72811536
Thanks! Not sure I understand what is happening, as up until the 3rd recursive call, things appeared to work, and that was without the return. Can you elaborate on why that made it work?
– jobechoi
Jan 3 at 3:34
calls heppen, they do. But if you don't executereturn
as I added, then your func will not return anything. Does it make sense now?
– quirimmo
Jan 3 at 3:35
return isn't called (it's not a function), it's executed. ;-)
– RobG
Jan 3 at 3:38
@RobG fair enough :D thanks, edited :)
– quirimmo
Jan 3 at 3:38
I sort of get it: the recursive call at some point has to return something. In my initial code, that wasn't happening. Thanks.
– jobechoi
Jan 3 at 3:42
|
show 1 more comment
Thanks! Not sure I understand what is happening, as up until the 3rd recursive call, things appeared to work, and that was without the return. Can you elaborate on why that made it work?
– jobechoi
Jan 3 at 3:34
calls heppen, they do. But if you don't executereturn
as I added, then your func will not return anything. Does it make sense now?
– quirimmo
Jan 3 at 3:35
return isn't called (it's not a function), it's executed. ;-)
– RobG
Jan 3 at 3:38
@RobG fair enough :D thanks, edited :)
– quirimmo
Jan 3 at 3:38
I sort of get it: the recursive call at some point has to return something. In my initial code, that wasn't happening. Thanks.
– jobechoi
Jan 3 at 3:42
Thanks! Not sure I understand what is happening, as up until the 3rd recursive call, things appeared to work, and that was without the return. Can you elaborate on why that made it work?
– jobechoi
Jan 3 at 3:34
Thanks! Not sure I understand what is happening, as up until the 3rd recursive call, things appeared to work, and that was without the return. Can you elaborate on why that made it work?
– jobechoi
Jan 3 at 3:34
calls heppen, they do. But if you don't execute
return
as I added, then your func will not return anything. Does it make sense now?– quirimmo
Jan 3 at 3:35
calls heppen, they do. But if you don't execute
return
as I added, then your func will not return anything. Does it make sense now?– quirimmo
Jan 3 at 3:35
return isn't called (it's not a function), it's executed. ;-)
– RobG
Jan 3 at 3:38
return isn't called (it's not a function), it's executed. ;-)
– RobG
Jan 3 at 3:38
@RobG fair enough :D thanks, edited :)
– quirimmo
Jan 3 at 3:38
@RobG fair enough :D thanks, edited :)
– quirimmo
Jan 3 at 3:38
I sort of get it: the recursive call at some point has to return something. In my initial code, that wasn't happening. Thanks.
– jobechoi
Jan 3 at 3:42
I sort of get it: the recursive call at some point has to return something. In my initial code, that wasn't happening. Thanks.
– jobechoi
Jan 3 at 3:42
|
show 1 more comment
Recursion is a functional heritage and so using it with functional style yields the best results. There's no "missing return
"; there's little need for return
in functional style as it is a side effect, it does not produce a value, it cannot be called like a function, and it cannot be combined with other expressions. Other imperative style statements like the reassignment/mutation div10 += 1
are also a recipe for a migraine when used recursively.
Your program can be dramatically simplified –
const numDigits = n =>
n < 10
? 1
: 1 + numDigits (Math .floor (n / 10))
console .log
( numDigits (1) // 1
, numDigits (22) // 2
, numDigits (333) // 3
, numDigits (999999999) // 9
)
The count
variable can still be used if you prefer. This would be a practical step toward making the program stack-safe –
const numDigits = (n, count = 1) =>
n < 10
? count
: numDigits
( Math .floor (n / 10)
, count + 1
)
console .log
( numDigits (1) // 1
, numDigits (22) // 2
, numDigits (333) // 3
, numDigits (999999999) // 9
)
"There's no "missing return"" might be a reasonable statement if you hadn't added an implicit return through use of an arrow function. There is indeed a missing return statement, your refactored code is effectivelyif (n < 10) {return count} else {return numDigits(Math.floor (n / 10), count + 1)}
. Removing the redundant else but keeping the return statement in the block is effectively identical to the OP's code. QED.
– RobG
Jan 3 at 8:52
The ternary is simple and succinct. As stated: new to recursion. I will keep this model in mind now. I see that my departure happened much earlier, even before the code was written: I never thought to check whether the integer was less than 10. Thanks, user633183.
– jobechoi
Jan 3 at 11:25
@RobG the difference lies between statements and expressions – statements likeif
,else
,foreach
,switch
,while
, andreturn
are side effects, do not produce a value, and cannot be composed with other statements. Worse,if
allows you to write conditionals without anelse
branch – ternary expressions using?:
evaluate to a value, can be composed with other expressions, and cannot be written without an "else" branch. It looks like I never got around to finishing this answer but I think you will find it useful.
– user633183
Jan 3 at 21:47
@RobG and for more about discussion about statements vs. expressions, I wish to share this. Any further discussion is welcomed and encouraged. The topics here are among my favourite :D
– user633183
Jan 3 at 22:11
add a comment |
Recursion is a functional heritage and so using it with functional style yields the best results. There's no "missing return
"; there's little need for return
in functional style as it is a side effect, it does not produce a value, it cannot be called like a function, and it cannot be combined with other expressions. Other imperative style statements like the reassignment/mutation div10 += 1
are also a recipe for a migraine when used recursively.
Your program can be dramatically simplified –
const numDigits = n =>
n < 10
? 1
: 1 + numDigits (Math .floor (n / 10))
console .log
( numDigits (1) // 1
, numDigits (22) // 2
, numDigits (333) // 3
, numDigits (999999999) // 9
)
The count
variable can still be used if you prefer. This would be a practical step toward making the program stack-safe –
const numDigits = (n, count = 1) =>
n < 10
? count
: numDigits
( Math .floor (n / 10)
, count + 1
)
console .log
( numDigits (1) // 1
, numDigits (22) // 2
, numDigits (333) // 3
, numDigits (999999999) // 9
)
"There's no "missing return"" might be a reasonable statement if you hadn't added an implicit return through use of an arrow function. There is indeed a missing return statement, your refactored code is effectivelyif (n < 10) {return count} else {return numDigits(Math.floor (n / 10), count + 1)}
. Removing the redundant else but keeping the return statement in the block is effectively identical to the OP's code. QED.
– RobG
Jan 3 at 8:52
The ternary is simple and succinct. As stated: new to recursion. I will keep this model in mind now. I see that my departure happened much earlier, even before the code was written: I never thought to check whether the integer was less than 10. Thanks, user633183.
– jobechoi
Jan 3 at 11:25
@RobG the difference lies between statements and expressions – statements likeif
,else
,foreach
,switch
,while
, andreturn
are side effects, do not produce a value, and cannot be composed with other statements. Worse,if
allows you to write conditionals without anelse
branch – ternary expressions using?:
evaluate to a value, can be composed with other expressions, and cannot be written without an "else" branch. It looks like I never got around to finishing this answer but I think you will find it useful.
– user633183
Jan 3 at 21:47
@RobG and for more about discussion about statements vs. expressions, I wish to share this. Any further discussion is welcomed and encouraged. The topics here are among my favourite :D
– user633183
Jan 3 at 22:11
add a comment |
Recursion is a functional heritage and so using it with functional style yields the best results. There's no "missing return
"; there's little need for return
in functional style as it is a side effect, it does not produce a value, it cannot be called like a function, and it cannot be combined with other expressions. Other imperative style statements like the reassignment/mutation div10 += 1
are also a recipe for a migraine when used recursively.
Your program can be dramatically simplified –
const numDigits = n =>
n < 10
? 1
: 1 + numDigits (Math .floor (n / 10))
console .log
( numDigits (1) // 1
, numDigits (22) // 2
, numDigits (333) // 3
, numDigits (999999999) // 9
)
The count
variable can still be used if you prefer. This would be a practical step toward making the program stack-safe –
const numDigits = (n, count = 1) =>
n < 10
? count
: numDigits
( Math .floor (n / 10)
, count + 1
)
console .log
( numDigits (1) // 1
, numDigits (22) // 2
, numDigits (333) // 3
, numDigits (999999999) // 9
)
Recursion is a functional heritage and so using it with functional style yields the best results. There's no "missing return
"; there's little need for return
in functional style as it is a side effect, it does not produce a value, it cannot be called like a function, and it cannot be combined with other expressions. Other imperative style statements like the reassignment/mutation div10 += 1
are also a recipe for a migraine when used recursively.
Your program can be dramatically simplified –
const numDigits = n =>
n < 10
? 1
: 1 + numDigits (Math .floor (n / 10))
console .log
( numDigits (1) // 1
, numDigits (22) // 2
, numDigits (333) // 3
, numDigits (999999999) // 9
)
The count
variable can still be used if you prefer. This would be a practical step toward making the program stack-safe –
const numDigits = (n, count = 1) =>
n < 10
? count
: numDigits
( Math .floor (n / 10)
, count + 1
)
console .log
( numDigits (1) // 1
, numDigits (22) // 2
, numDigits (333) // 3
, numDigits (999999999) // 9
)
const numDigits = n =>
n < 10
? 1
: 1 + numDigits (Math .floor (n / 10))
console .log
( numDigits (1) // 1
, numDigits (22) // 2
, numDigits (333) // 3
, numDigits (999999999) // 9
)
const numDigits = n =>
n < 10
? 1
: 1 + numDigits (Math .floor (n / 10))
console .log
( numDigits (1) // 1
, numDigits (22) // 2
, numDigits (333) // 3
, numDigits (999999999) // 9
)
const numDigits = (n, count = 1) =>
n < 10
? count
: numDigits
( Math .floor (n / 10)
, count + 1
)
console .log
( numDigits (1) // 1
, numDigits (22) // 2
, numDigits (333) // 3
, numDigits (999999999) // 9
)
const numDigits = (n, count = 1) =>
n < 10
? count
: numDigits
( Math .floor (n / 10)
, count + 1
)
console .log
( numDigits (1) // 1
, numDigits (22) // 2
, numDigits (333) // 3
, numDigits (999999999) // 9
)
edited Jan 3 at 7:12
answered Jan 3 at 6:59
user633183user633183
72k21143184
72k21143184
"There's no "missing return"" might be a reasonable statement if you hadn't added an implicit return through use of an arrow function. There is indeed a missing return statement, your refactored code is effectivelyif (n < 10) {return count} else {return numDigits(Math.floor (n / 10), count + 1)}
. Removing the redundant else but keeping the return statement in the block is effectively identical to the OP's code. QED.
– RobG
Jan 3 at 8:52
The ternary is simple and succinct. As stated: new to recursion. I will keep this model in mind now. I see that my departure happened much earlier, even before the code was written: I never thought to check whether the integer was less than 10. Thanks, user633183.
– jobechoi
Jan 3 at 11:25
@RobG the difference lies between statements and expressions – statements likeif
,else
,foreach
,switch
,while
, andreturn
are side effects, do not produce a value, and cannot be composed with other statements. Worse,if
allows you to write conditionals without anelse
branch – ternary expressions using?:
evaluate to a value, can be composed with other expressions, and cannot be written without an "else" branch. It looks like I never got around to finishing this answer but I think you will find it useful.
– user633183
Jan 3 at 21:47
@RobG and for more about discussion about statements vs. expressions, I wish to share this. Any further discussion is welcomed and encouraged. The topics here are among my favourite :D
– user633183
Jan 3 at 22:11
add a comment |
"There's no "missing return"" might be a reasonable statement if you hadn't added an implicit return through use of an arrow function. There is indeed a missing return statement, your refactored code is effectivelyif (n < 10) {return count} else {return numDigits(Math.floor (n / 10), count + 1)}
. Removing the redundant else but keeping the return statement in the block is effectively identical to the OP's code. QED.
– RobG
Jan 3 at 8:52
The ternary is simple and succinct. As stated: new to recursion. I will keep this model in mind now. I see that my departure happened much earlier, even before the code was written: I never thought to check whether the integer was less than 10. Thanks, user633183.
– jobechoi
Jan 3 at 11:25
@RobG the difference lies between statements and expressions – statements likeif
,else
,foreach
,switch
,while
, andreturn
are side effects, do not produce a value, and cannot be composed with other statements. Worse,if
allows you to write conditionals without anelse
branch – ternary expressions using?:
evaluate to a value, can be composed with other expressions, and cannot be written without an "else" branch. It looks like I never got around to finishing this answer but I think you will find it useful.
– user633183
Jan 3 at 21:47
@RobG and for more about discussion about statements vs. expressions, I wish to share this. Any further discussion is welcomed and encouraged. The topics here are among my favourite :D
– user633183
Jan 3 at 22:11
"There's no "missing return"" might be a reasonable statement if you hadn't added an implicit return through use of an arrow function. There is indeed a missing return statement, your refactored code is effectively
if (n < 10) {return count} else {return numDigits(Math.floor (n / 10), count + 1)}
. Removing the redundant else but keeping the return statement in the block is effectively identical to the OP's code. QED.– RobG
Jan 3 at 8:52
"There's no "missing return"" might be a reasonable statement if you hadn't added an implicit return through use of an arrow function. There is indeed a missing return statement, your refactored code is effectively
if (n < 10) {return count} else {return numDigits(Math.floor (n / 10), count + 1)}
. Removing the redundant else but keeping the return statement in the block is effectively identical to the OP's code. QED.– RobG
Jan 3 at 8:52
The ternary is simple and succinct. As stated: new to recursion. I will keep this model in mind now. I see that my departure happened much earlier, even before the code was written: I never thought to check whether the integer was less than 10. Thanks, user633183.
– jobechoi
Jan 3 at 11:25
The ternary is simple and succinct. As stated: new to recursion. I will keep this model in mind now. I see that my departure happened much earlier, even before the code was written: I never thought to check whether the integer was less than 10. Thanks, user633183.
– jobechoi
Jan 3 at 11:25
@RobG the difference lies between statements and expressions – statements like
if
, else
, foreach
, switch
, while
, and return
are side effects, do not produce a value, and cannot be composed with other statements. Worse, if
allows you to write conditionals without an else
branch – ternary expressions using ?:
evaluate to a value, can be composed with other expressions, and cannot be written without an "else" branch. It looks like I never got around to finishing this answer but I think you will find it useful.– user633183
Jan 3 at 21:47
@RobG the difference lies between statements and expressions – statements like
if
, else
, foreach
, switch
, while
, and return
are side effects, do not produce a value, and cannot be composed with other statements. Worse, if
allows you to write conditionals without an else
branch – ternary expressions using ?:
evaluate to a value, can be composed with other expressions, and cannot be written without an "else" branch. It looks like I never got around to finishing this answer but I think you will find it useful.– user633183
Jan 3 at 21:47
@RobG and for more about discussion about statements vs. expressions, I wish to share this. Any further discussion is welcomed and encouraged. The topics here are among my favourite :D
– user633183
Jan 3 at 22:11
@RobG and for more about discussion about statements vs. expressions, I wish to share this. Any further discussion is welcomed and encouraged. The topics here are among my favourite :D
– user633183
Jan 3 at 22:11
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%2f54015936%2fmuch-frustrated-gnashing-from-undefined-function-return-type%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