+ 1

JAVA How this code work?

Isnt this code output 234 only? Why 2344 https://code.sololearn.com/cE9V6TM7EnmQ/?ref=app

11th Aug 2019, 2:25 AM
Tzion
Tzion - avatar
6 ответов
+ 7
For each iteration of the recursive function, it returns a value that increments every iteration if that value is less than 4 (the value of f). At the start, the function returns 2 from the else clause, then returns 3 from the else clause, and lastly 4, once again from the else clause. At this point, the value of i is 5, and because it is greater than the value of f, it returns f as a string, printing out the second 4 within the output
11th Aug 2019, 2:29 AM
Faisal
Faisal - avatar
+ 4
Rael Well, because the test function is returning a string, you can look at it as if it were just a regular value being added to i. In a way, return (i + test(i+1,f)) can be looked at as return (2 + test(2+1,f)), which will lead to this: return (2 + (3 + test(4,f))), return (2 + (3 + (4 + test(5,f)))), and lastly: return (2 + (3 + (4 + ("" + 4))))
11th Aug 2019, 2:47 AM
Faisal
Faisal - avatar
+ 2
Whoops! Sorry, just fixed my response haha
11th Aug 2019, 5:28 AM
Faisal
Faisal - avatar
0
Faisal the i became 5 was from return (i + test(i+1,f)) ? return(2 + test(3,4))? 2+3 = 5 if yes,i wanna ask why the "f" doesnt involve in the plus?
11th Aug 2019, 2:39 AM
Tzion
Tzion - avatar
0
its 2344
11th Aug 2019, 5:27 AM
Tzion
Tzion - avatar
0
nice!
11th Aug 2019, 4:35 PM
Juan Debenedetti
Juan Debenedetti - avatar