import {Component, OnInit} from '@angular/core';

@Component({
  selector: 'app-fibonacci',
  templateUrl: './fibonacci.component.html',
  styleUrls: ['./fibonacci.component.css']
})
export class FibonacciComponent implements OnInit {

  input: string = '';
  result: string = '';
  error: string = '';

  constructor() {}

  ngOnInit(): void {
  }

  updateMessage(num: HTMLInputElement) : boolean {
    const input = parseInt(num.value);
    if (isNaN(input) || input < 1) {
      this.error = 'Enter a valid positive integer!';
    } else {
      this.error = '';
    }
    this.result = this.calculate(input).toString();
    return false;
  }

  calculate(num: number) : number {
    const sequence: number[] = [0, 1];
    for(let i = 2; i < num; i++) {
      sequence.push(sequence[sequence.length - 1] + sequence[sequence.length - 2]);
    }
    return sequence[num-1];
  }

}
