Code Lab

Modo entrevista. Cronômetro de 10 min, sem dicas e sem autocompletar

Fibonacci numbers

FácilBasics10 min

Write a function fibonacci(n) that returns an array with the first n Fibonacci numbers.

  • The sequence starts with 0 and 1. Every next number is the sum of the two numbers before it: 0, 1, 1, 2, 3, 5, 8, 13, ...
  • The parameter n is an integer from 0 to 50. For 0 return an empty array [].

Then write a second function fibonacciBetween(min, max). It returns every Fibonacci number x with min <= x <= max, in the order of the sequence.

  • 1 is in the sequence twice, keep both: fibonacciBetween(1, 1) returns [1, 1].
  • If no number fits, return [].
  • min and max are integers, 0 <= min <= max <= 1000000.

Exemplos

fibonacci(7)

Retorna [ 0, 1, 1, 2, 3, 5, 8 ]

Long arrays are shown on several lines, the same way Node prints them.
fibonacci(1)

Retorna [ 0 ]

fibonacciBetween(50, 100)

Retorna [ 55, 89 ]

Cobrada em Accion Labs · Fonte

Carregando o editor...
  1. Entradafibonacci(7)
    Esperado[ 0, 1, 1, 2, 3, 5, 8 ]
  2. Entradafibonacci(1)
    Esperado[ 0 ]
  3. fibonacciBetween(50, 100) returns [55, 89]
  4. Entradafibonacci(0)
    Esperado[]

Os testes ocultos rodam no envio