Doubt in a code to calculate the growth of a tree

Asked

Viewed 286 times

0

I am having some difficulties to make this code below work. The statement of the exercise is this:

Utopian trees grow in a particular way in two cycles:

  • Each spring doubles its size
  • each summer grow a meter

If Laura plants a one meter utopian tree in late autumn, what would be her height after N cycles?

Some examples:

if N = 0, its height will be 1 meter (nothing has grown); si N = 1, its height will be 2 meters (doubled the height in spring); si N = 2, its height will be 3 meters (grew one meter more in summer); si N = 3, its height will be 6 meters (doubled the height in the next spring); And so ...

Write the function alturaArvoreUtopica, that uses an amount of growth cycles, and returns the resulting height of Laura’s tree.

There’s that code below:

function alturaArvoreUtopica(ciclos) {

const cicloInicial = 1;
const tamanhoInicial = 1;
const estacaoInicial = 0;

const estacoes = ['verao', 'primavera']
let tamanho = tamanhoInicial

for (let i = 0 + estacaoInicial; i < cicloInicial + estacaoInicial; i++) {
const estacao = estacoes[i % estacoes.length]

if (estacao === 'verao') tamanho += 1
if (estacao === 'primavera') tamanho *= 2
}

return tamanho
}

But it is presenting the following errors:

Your solution did not pass the tests Objectives that were not met: alturaArvoreUtopica makes comparisons against strings Results of the test: alturaArvoreUtopica(0) 2 == 1

I would like to know where I am going wrong in the code. Thanks in advance!

  • Ah detail also, being more specific, the idea would be that the tree grows only in spring and summer. In spring it grows twice as tall, and in summer it grows another metre.

  • Possible duplicate of: Dual in function and for

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.