Problemas con un ejercicio! repeat + for


#1

Los árboles utópicos crecen de una forma particular, en dos ciclos:

  • cada primavera duplican su tamaño
  • cada verano crecen un metro

Si Laura planta un árbol utípico de un metro, al fin del otoño, ¿cuanto medirá de alto despuésde N ciclos?

Algunos ejemplos:

  • si N = 0, su altura será 1 metro (no creció nada)
  • si N = 1, su altura será de 2 metros (duplicó su altura en primavera)
  • si N = 2, su altura será de 3 metros (creció un metro más en verano)
  • si N = 3, su altura será de 6 metros (duplicó su altura en la siguiente primavera)
  • y así …

Escribí la función alturaArbolUtopico , que tome una cantidad de ciclos de crecimiento, y devuelva la altura resultante del arbol de Laura.


function alturaArbolUtopico(n){
  var crecimientoTotal = 1;

  for(var i = 1; i < n; i = i+2) {
   crecimientoTotal = crecimientoTotal + alturaArbolUtopico(i) * 2 + 1;   
}
 
 
return crecimientoTotal;
  
  }
  

#2

Without recursion, only augmentation, we get preliminary results that show no change in summer.

> function treeHeight(n) {
    var totalGrowth = 1;
    for (var i = 1; i < n; i += 2) {
      totalGrowth += i * 2 + 1;
    } 
    return totalGrowth;  
  }

#3

Thanks I get it, but still something is not working :frowning:

alturaArbolUtopico(1)
1 == 2
alturaArbolUtopico(2)
4 == 3
alturaArbolUtopico(3)
4 == 6
alturaArbolUtopico(4)
11 == 7


#4

Please show us your revised code.

Run this line and the check the results against the list.

[0,1,2,3,4,5,6,7,8,9,10].forEach(n => console.log(`n=${n} h=${treeHeight(n)}`))
n=0 h=1
n=1 h=2
n=2 h=3
n=3 h=6
n=4 h=7
n=5 h=14
n=6 h=15
n=7 h=30
n=8 h=31
n=9 h=62
n=10 h=63

#5

im lost here

solution.js:24
var mumuki_query_result = [0,1,2,3,4,5,6,7,8,9,10].forEach(n => console.log(n=${n} h=${treeHeight(n)}));
^

ReferenceError: treeHeight is not defined
at solution.js:24:93
at Array.forEach (native)
at Object.<anonymous> (solution.js:24:56)
at Module._compile (module.js:409:26)
at Object.Module._extensions…js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3


#6

Be sure you run that line after the function.

function treeHeight(n) {

}
[0,1,2,3,4,5,6,7,8,9,10].forEach(n => console.log(`n=${n} h=${treeHeight(n)}`));

Don’t assign that line to a variable.


#7

it runs but shows

alturaArbolUtopico(0)
undefined == 1

in every test, its supose ti be a simple excercise, i dont understend why didnt aply the summer growth in the fist response you send, maybe mumuki hates me


#8

in fact is not aplying the spring growth


#9

Not sure this problem can be solved with a simple equation. It’s an either or sort of problem. If we iterate i=1 through i=n, growth starting at 1,

when i is odd, double the growth, else add 1 to growth.

#10

i alredy try to use i%2!=0 to filter the spring cicles but the function dont run


#11

I alredy have enough excersices to aprove but this one i take it personal jajajaj


#12

Do you want a spoiler to study?


#13

I want to understend above all the things in the world (intense girl with a lingering brain)


#14

If you wish to use the earlier test line, then write this with a return. Below I’ve written it as a callback…

treeHeight.js
function treeHeight(n) {
  growth = 1;
  for (i = 1; i <= n; i++) {
    if (i % 2) {
      growth *= 2;
    } else {
      growth += 1;
    }
  }
  console.log(`n=${n} h=${growth}`);
}
[0,1,2,3,4,5,6,7,8,9,10].forEach(treeHeight)