Php school work

Distribute a total amount randomly (within certain parameters) in a range of dates, excluding weekends with a baseline that will define the minimum amount of the value assigned to a specific date. The returned result should be a unidimensional array with the following structure:

Array( [YYYY-mm-dd]=>0.00, // The value should be a float value value (with two decimal places)corresponding to the date used as key, [YYYY-mm-dd]=>0.00,…)

Requirements:

  1. The values should be distributed only on weekdays and the weekends should be included in the array and have zero as value assigned.

  2. The total sum of the values assigned in the resulting array should be within 1% above or below the total value given.
    Example:
    $total=75300;
    $distributed=array_sum($result);
    // $distributed value can only be a maximum of 76053 or a minimum of 74547 (1% above a!nd below 75300)

  3. Implementation of object programming is preferred in this exercise

  4. The baseline parameter should control the amount of randomness in the distribution of values throughout
    the given dates and it should be given as an integer from 0 to 100. It should also guarantee a minimum value per
    date. The baseline indicates the amount of randomness allowed in the distribution. This method can have several
    approaches depending on the algorithm used, so the actual value resulting of the baseline is open to interpretation. In
    the following examples we use it as the percentage of the total, divided amongst the total of days that have non-zero
    values (weekdays).
    Example 1:
    $baseline=20;
    $total=100;
    $start_date=‘2016-12-19’;
    $start_date=‘2016-12-23’;
    // This set of parameters should guarantee that each day will have a minimum value of 4
    // Example result
    Array(
    [2016-12-19]=>12,
    [2016-12-20]=>4.22,
    [2016-12-21]=>34.53,
    [2016-12-22]=>19.47,
    [2016-12-23]=>29.78
    )

  5. All dates within the range should be included in the array.

Firstly, welcome to the forums.

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://freecodecamp.org.

With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).

It is pretty typical on here for people to share a codepen / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.

Please provide some example of what you’ve tried and I’m sure you’ll get more help.

Happy coding :slight_smile:

Thank you, i have not found this tuto. We started with this in the class but without much of the explanation. I am learning with the freecodecamp parelly with my studies. T

function getNumWeekdays($start_day, $end_day)

{

$start_day_ts = strtotime($start_day);

$end_day_ts = strtotime($end_day);

$num_weekdays = 0;

while ($start_day_ts <= $end_day_ts ) {

if (date(“N”, $start_day_ts) < 6){

++$num_weekdays;

}

$start_day_ts += 86400;

}

return $num_weekdays;

}

function getArrayRandNumbers($num_week_days){

$total = 0;

$rand_num = 0;

$weekday_array = array();

$norm_array = array();

for ($i= 0 ; $i < $num_week_days ; $i++) {

$rand_num = mt_rand(1,1000);

$total += $rand_num;

array_push($weekday_array , $rand_num);

}

$total = floatval($total);

foreach ( $weekday_array as $wd ) {

array_push( $norm_array, floatval($wd) / $total);

}

return $norm_array;

}

function dist_amount_randomly($amount, $baseline, $start_date, $end_date){

$start_day_ts = strtotime($start_date);

$end_day_ts = strtotime($end_date);

$num_weekdays = getNumWeekdays($start_date, $end_date);

$num_weekdays

$norm_array = getArrayRandNumbers($num_weekdays);

$index_of_norm_array = 0;

$output_array = array();

$min_in_days = floatval($amount)*floatval($baseline)/(floatval($num_weekdays)*100.0);

$amount_to_be_dist = floatval(100-$baseline) *floatval($amount)/100.0;

while ($start_day_ts <= $end_day_ts ) {

if (date(“N”, $start_day_ts) < 6){

$amount_for_weekday = $norm_array[$index_of_norm_array]*$amount_to_be_dist+$min_in_days;

$formatted_amount = number_format($amount_for_weekday, 2,’.’,’’);

$index_of_norm_array++;

} else {

$formatted_amount = “0.00”;

}

And without an idea how to change it to array.