-1
Utopian trees grow in a particular way in two cycles:
Each spring doubles its size each summer grow a meter
A tree planted with one meter in late autumn, what would be its height after N cycles?
Some examples:
- If N = 0, your height will be 1 meter (nothing has grown)
- If N = 1, your height will be 2 meters (doubled the height in spring)
- If N = 2, its height will be 3 meters (grew one meter more in summer)
- If N = 3, its height will be 6 meters (doubled the height 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.
I tried that and it didn’t work :
function alturaArvoreUtopica(ciclos) {
let tamanhoArvore = 1;
for(let i = 0; i < ciclos; i++)
if(i = 1){
tamanhoArvore += 1;}
else{
tamanhoArvore *= 2;}
return tamanhoArvore;
}
alturaArvoreUtopica(4)
What have you tried? post in question...
– vik
when the
i
is odd fold height, when pair grows 1, you can know if thei
is even or not using module operator%
ex:if (i % 2 === 0) // é par
... begin with thei
to 1– vik
@Vik has become so Function alturaArvoreUtopica(cycles) {Let sizeArvore = 1; for(Let i = 0; i < cycles; i++) if(i % 2 ===0)cheg sizeArvore += 1;} Else! sizeArvore *= 2;} Return sizeArvore;} but still giving error , it only accepts whether the cycle is 0 or 1, the other numbers when it is tested wrong. I tried starting i =1 and tbm didn’t work.
– bianca
must begin with the
i
to 1, and be up toi <= ciclos
– vik
@Vik, now it worked, I changed to i <=cycles.
– bianca
@Vik creates an answer to be accepted and finalize the question
– Costamilam