Utopian Tree Exercise, Tree of Life etc. etc

Asked

Viewed 245 times

-1

Good morning! I am taking the Santander course, and the last challenge is the famous calculation of the height of the tree of life. I have a very specific problem. Rather, it follows the statement:

Utopian trees grow in a particular way in two cycles:

each spring double its size each summer grow one meter if Laura plant a utopian tree with one meter, in late autumn, which would be its height after N cycles?

Some examples:

si N = 0, your 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 spring next)

And so ... Write the function alturaArvoreUtopica, which uses a amount of growth cycles, and return the height resulting from the laura tree.

So, since the platform is kind of bad, I usually do the code in Codepen, test and then move to the platform in the course. My code looked like this:

function alturaArvoreUtopica (ciclos) {
  var altura = 1;
  if (ciclos == 0) {
    altura = 1;
  } else {
  for (var i=1; i<=ciclos; i++) {
    if (i % 2 == 0) {
      altura += 1;
    } else {
      altura = altura*2;
    }
    }
    return altura  
  }}

In Codepen the code worked well in all tests, however, when running the code on the platform appeared the following error: Expected Undefined to Equal 1.

Does anyone know what might have happened? Is it some detail I missed or should I redo the code differently?

  • Your question is about a third-party testing tool. Nor is it about a standardized testing tool. The question the use of this test tool should be asked to those who made the tool or who are using it as a teaching method.

1 answer

0


You forgot the Return for case 0. Change to:

function alturaArvoreUtopica (ciclos) {
  var altura = 1;
  if (ciclos == 0) {
    altura = 1;
    return altura; // aqui
  } else {
    for (var i=1; i<=ciclos; i++) {
      if (i % 2 == 0) {
        altura += 1;
      } else {
        altura = altura*2;
      }
    }
    return altura;
  }
}

console.log(alturaArvoreUtopica(0))

From what you said and the message, your code probably went through a test cycle with different values. When you call the original function with the value 0 the return is coming Undefined. It should be just that. Note that the last Return is inside Else.

Doing the function like this makes it easier to read and understand:

function alturaArvoreUtopica (ciclos) {
  var altura = 1;
  if (ciclos == 0) 
    altura = 1;
  else
    for (var i=1; i<=ciclos; i++) {
      if (i % 2 == 0)
        altura += 1;
      else
        altura = altura*2;
    }
  return altura;
}

And this is for you to study a little:

let alturaArvoreUtopica2 = (ciclos) => {
  let altura = 1;
  if (!ciclos) 
    return 1;

  [...Array(++ciclos).keys()]
    .slice(1)
    .forEach((_, i)=>(i + 1)%2 ? altura *= 2 : altura++);
  return altura;
}
  • 1

    It worked! It really was a lack of attention from me. Thank you also for the tip in the organization of the code, as I’m starting I end up not paying attention to it and now I realize how my code was pretty messed hahaha

  • I’m glad it worked out... I edited the answer with a more advanced way to encourage you to progress a little in js :)

  • Often code indentation causes you to misunderstand the code. For example, in its original function it implies that the last Return is before the output of the function when it is inside Else. One thing that helps a lot is using an IDE that has an automatic code formatter. :)

Browser other questions tagged

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