Pages

DM : Algo

 Voici le code d'une fonction  baseRange.

baseRange.js

  1. const baseRange = function (start, end=start, step = 1, fromRight = false) {


  2.     let index = -1

  3.        , length = Math.max(Math.ceil((end - start) / (step)), 0)

  4.        , result = Array(length);


  5.     while (length--) {

  6.         result[fromRight ? length : ++index] = start;

  7.         start += step;

  8.     }

  9.     return result;

  10. }


Help people interested in this repository understand your project by adding a README.

Ecrire un README qui donne les cas d'utilisation de la fonction  baseRange.js

Trouvez les valeurs par défaut pour obtenir le comportement suivant :

const baseRange = function ({
  start = ?,
  end = ?,
  step = ?,
  fromRight = ?,
} = {}) {
  let index = -1,
    length = Math.max(Math.ceil((end - start) / step), 0),
    result = Array(length);

  while (length--) {
    result[fromRight ? length : ++index] = start;

    start += step;
  }

  return result;
};

console.log(baseRange()); // [ 0 ]
console.log(baseRange({ start: 100 })); // [100];
console.log(baseRange({ start: 0, end: 10, fromRight: 1 })); //[9, 8, 7, 6, 5, 4, 3, 2, 1, 0];
console.log(baseRange({ start: 0, end: 10, step: 2 })); // [ 0, 2, 4, 6, 8 ]