GithubHelp home page GithubHelp logo

api-rest-fibonacci-nestjs's Introduction

Nest Logo

API REST FIBONACCI

Este proyecto esta hecho en NestJS, primero instale las dependencias:

npm i

Una vez instalada las dependencias inicie el server ejecutando el siguiente comando:

npm run start

Luego puede realizar la petición desde http://localhost:3000/fibonacci/{index}

Dentro del service AppService, en el constructor de la clase se llama al metodo generateFibonacci, el cual cargara un array llamado _arrayFib con los valores iniciales de la secuencia ([0, 1, 1, 2, 3, 5, 8, 13]). Para ello uso una variable llamada limit la cual inicia en 8.

  private _limit = 8;

  private _generateFibonacci(start: number, addNewValue = false) {
    for (let i = start; i < this._limit; i++) {
      const value = this._arrayFib[i - 1] + this._arrayFib[i - 2];
      if (addNewValue) {
        this._arrayFib.push(value);
      } else {
        this._arrayFib[i] = value;
      }
    }
  }

Cuando se inicia el service, se cargaran los valores iniciales :

  constructor() {
    this._generateFibonacci(2);
  }

En la función getFibonacci se captura el indice enviado de la petición, primero verificamos que exista un valor en nuestro array _arrayFib, si existe retornamos el valor, de lo contrario lo ideal es volver a calcular el limite en base al indice enviado, de esa manera iremos cargando solo los nuevos valores necesarios al array _arrayFib sin necesidad de volver a generar todas las secuencias desde cero.

getFibonacci(index: number): number {
    const value = this._arrayFib[index];
    if (value) {
      return value;
    }
    const newLimit = Number(index) + 1 - this._limit;
    this._limit = this._limit + newLimit;

    this._generateFibonacci(this._arrayFib.length, true);

    return this._arrayFib[index];
  }

api-rest-fibonacci-nestjs's People

Contributors

jimyhdolores avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.