Countdown from 100 to 0 without assignments in PHP

I am having trouble figuring out an exercise that my boss gave me as a personal enrichment. Unfortunately, I have searched all over the internet for an answer without success, and now I am reaching out to you programmers.

He asked me to do a simple countdown from 100 to 0 (it can display right away). Easy enough so far? Just keep it simple for a loop or even time. The problem here is that it asks that there was no purpose, for example in the code: $[var]=[value]

.

How can you create a loop without redirects? Since it $i--;

is equivalent $i = $i - 1;

, how can we count?

I am puzzled by this problem that I cannot solve, I really want to find an answer as I am very curious how this can be done.

Help is kindly appreciated.

Edit

Note that this problem is kind of 2nd part where the first part is to get it to work from 100 to 0 and the second part is from x to 0 where x is entered by the user

+3


source to share


2 answers


My guess is that your boss is trying to encourage you to recursion , not iterate. Recursion, as a technique, works very well for certain problems and is a great tool for your weapon as a programmer.

Without giving away everything, try experimenting with defining a function that takes an argument - a countdown number - and then calls itself in some way. You will also need to run it separately by calling it as soon as you have defined it.



Keep in mind that recursion must have a certain constraint, otherwise things can go very wrong. Here's an example of something really bad to get you started :: D

<?php
  function infinity() {
    print "Whoah.";
    infinity();
  }
  infinity();

      

+3


source


I'll give you some tips, writing the exact code is an exercise for you.



  • There are no assignments, so you must have a type that can hold multiple values. Primitive types like integers cannot be used directly here, think of arrays.

  • Now you cannot use variables, so using a loop and printing values ​​is prohibited. Use one of the many ways to print an array (or the result of an array).

  • The function range()

    does not accept a negative step value, you need to reverse the numbers.

+4


source







All Articles