I’m having a problem figuring out an exercise my boss gave me as personal enrichment of knowledge sort of. Unfortunately I have been looking for a glimpse of answer everywhere on the web with no success and I’m now turning to you fellow programmers.
What he asked me to do is make a simple countdown from 100 to 0 (it can be displayed all at once). Easy enough so far eh? Just make a simple for loop or a while even. The problem here is that he asks that there are no assignations in the code, ex: $[var]=[value].
How can one even make a loop with no assignations? Since $i--; is the equivalent to $i = $i - 1; how can we count down?
I’m baffled by this problem which I can’t resolve, I really want to find the answer as I am very curious on how this can be done.
Help is kindly appreciated.
Edit
Take note that this problem is kind of a 2 part where as the first part is to make it work 100 to 0, and in the second part x to 0 where x is inputted by the user
My guess would be that your boss is trying to encourage you towards recursion rather than iteration. Recursion, as a technique, works extremely well for certain problems, and is an excellent tool to have in your armoury as a programmer.
Without giving everything away, try experimenting with defining a function that takes an argument – the “countdown” number — and then calls itself in some way. You’ll also need to separately kick it off by calling it once you’ve defined it.
Bear in mind that recursion must have some kind of termination defined, otherwise things can go very wrong. Here’s an example of something going very wrong to get you started: 😀