+ 1

How do I solve the C# Module 3 quiz? (Level Points)

Level Points Passing the first level of a video game awards the player 1 point. For each subsequent level passed, the points awarded increment by 1 (2 for the 2nd level, 3 for the 3rd, and so on). The program you are given takes the number of passed levels as input. Complete the given function to take that number as an argument, and recursively calculate and return the total number of points given for all passed levels. Sample Input 3 Sample Output 6 Explanation Level 1: 1 point Level 2: 2 points Level 3: 3 points Total: 1+2+3 = 6 points.

29th Jun 2021, 6:58 AM
Shafe Hayat Khan
Shafe Hayat Khan - avatar
3 Respostas
+ 3
First try to do it your self then ask for help. Don't look for the entire answer because in order to become a better programmer you have to challenge your mind.
29th Jun 2021, 7:04 AM
Lemi Elias
Lemi Elias - avatar
+ 1
a recursive function is a function wich call itself... to be able to stop calling itself, the function must have a base case where it not call itself (instead return value) the first call of the required function get the number of levels... you must test if the number of levels is the first one level (base case), and if so return 1... else you must return the level number plus the result of calling the function with the level number minus one to get the recursive points result ;P
29th Jun 2021, 7:33 AM
visph
visph - avatar
0
One line solution using lambdas and ternary operator static Func<int, int> Points = levels => levels == 1 ? 1 : levels + Points(levels - 1);
20th Aug 2021, 7:46 AM
David García Prados