Dual in function and for

Asked

Viewed 4,637 times

0

I need to settle this matter:

Utopian trees grow in a particular way in two cycles: every spring they double their size, every summer they grow one meter. If Laura plants a utopian tree with one meter 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);
  • if N = 1, its 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).

I can’t tell if my code is being made right, but follow it below:

function alturaArvoreUtopica(){
  var ciclos
  var tamanhoArvore = 1
  for(var i = 0; i <= ciclos; i++){
    if(????){
      return tamanhoArvore *= 2
    }else
      return tamanhoArvore +=1
  }
}
  • "Spring" and "summer" represent how many months? Your question is a little vague. Could you provide a little more detail, please?

  • Hi Luiz, follow the question below: Utopian trees grow in a particular way, in two cycles: each spring double their size each summer grow one meter If Laura plants a utopian tree with one meter, In late fall, what would be your height after N cycles? Some examples: if N = 0, its height will be 1 meter (did not grow at all) if N = 1, its 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)

  • In autumn and winter the tree does not grow?

  • No, only in the spring and summer cycles

  • The height receives + 1 in summer and in spring the height is multiplied by 2?

  • Exactly that Luiz

Show 1 more comment

6 answers

4


This "cycle" is weird. By the question I would understand that a cycle would be a season, but in the example the tree grows throughout cycle, which suggests that a cycle would be summer + autumn and winter + spring. Then in that case the tree would grow alternately.

function alturaArvoreUtopica(ciclos) {
  let tamanhoArvore = 1;
  for(let i = 0; i < ciclos; i++)
    if(i & 1)
      tamanhoArvore += 1;
    else
      tamanhoArvore *= 2;

  return tamanhoArvore;
}


console.log(alturaArvoreUtopica(0));
console.log(alturaArvoreUtopica(1));
console.log(alturaArvoreUtopica(2));
console.log(alturaArvoreUtopica(3));

  • What counts only in the tree cycle is that it grows only 2x its size in spring and +1 in summer, it does not grow in the autumn and winter seasons

  • I understood this, but the definition of "cycle" was not clear.

  • The statement was a little confusing for me too, but from what I understood it is the following: This tree it grows only in the summer and spring seasons, being respectively +1 meter in summer and 2x meter in spring, can be N cycles, I just need to know the final size of it

  • What it means if( i &1 ) that is in the code?

3

Assuming:

  • The tree begins with 1 metro;
  • Growth begins in the summertime;
  • There are four stations, arranged in the following order: Verão -> Outono -> Inverno -> Primavera
  • And that growth takes place so that:
    • Summer: Tree size increases in 1;
    • Autumn: size is not changed;
    • Winter: size is not changed;
    • Spring: size is multiplied by 2.

We can stipulate that if we want 9 cycles to occur, the final size will be 11, since:

+---------------------+-----------+-----------+-----------+-----------+-----------+-----------+------------+-------------+
| Verão               | Outono    | Inverno   | Primavera | Verão     | Outono    | Inverno   | Primavera  | Verão       |
+---------------------+-----------+-----------+-----------+-----------+-----------+-----------+------------+-------------+
| 1 (inicial) + 1 = 2 | 2 + 0 = 2 | 2 + 0 = 2 | 2 * 2 = 4 | 4 + 1 = 5 | 5 + 0 = 5 | 5 + 0 = 5 | 5 * 2 = 10 | 10 + 1 = 11 |
+---------------------+-----------+-----------+-----------+-----------+-----------+-----------+------------+-------------+

The code to calculate that would be:

function utopicTree(cycles = 1, initialLength = 1, seasonStart = 0) {
  const seasons = ['summer', 'autumn', 'winter', 'spring']
  let currentLength = initialLength

  for (let i = 0 + seasonStart; i < cycles + seasonStart; i++) {
    const currentSeason = seasons[i % seasons.length]

    if (currentSeason === 'summer') currentLength += 1
    if (currentSeason === 'spring') currentLength *= 2
  }

  return currentLength
}

// Ciclos = 9 / Tamanho inicial = 1 / Começa na estação = 0 (verão)
console.log(utopicTree(9, 1, 0))


Note that if you want to disregard the "autumn" and "winter" cycles, simply remove them from the array of stations (Asons):

function utopicTree(cycles = 1, initialLength = 1, seasonStart = 0) {
  const seasons = ['summer', 'spring']
  let currentLength = initialLength

  for (let i = 0 + seasonStart; i < cycles + seasonStart; i++) {
    const currentSeason = seasons[i % seasons.length]

    if (currentSeason === 'summer') currentLength += 1
    if (currentSeason === 'spring') currentLength *= 2
  }

  return currentLength
}

// Ciclos = 6 / Tamanho inicial = 1 / Começa na estação = 0 (verão)
console.log(utopicTree(6, 1, 0))

1

function alturaArvoreUtopica(nCiclos){// nCiclos é a quantidade de ciclos da arvore  
    var alturaResultante;  
    for(var i = 0; i <= nCiclos; i++){  
        if(i == 0){ // se o contador é igual a zero retorna o valor inicial da arvore  
            alturaResultante = 1;  // valor inicial da arvore  = 1
        }  
        if(i %2 == 0&& i != 0){  // se contador diferente de zero e par "mod=0"
            alturaResultante+=1;  // soma-se 1 metro à altura da arvore
        }  
        if(i % 2 != 0 && i != 0){  //se contador diferente de zero e impar "mod!=0"
            alturaResultante*=2;  // dobra o tamanho da arvore
        }  
    }  
    return alturaResultante;  
}  
console.log(alturaArvoreUtopica(3)); 

1

I know this publication has been a while, but I came across this scenario and I was hard to understand too, the only one I could understand more or less was the reply of José Tomas, but I had an expression that I did not understand and had never seen if(i % 2), I researched and understood, so it got easier and this expression can be understood this way: if (i % 2 == 0). That would be the rest of the multiplication by two (picking up the even cycles) so it was like this:

function alturaArvoreUtopica(n) {
let alturaArvore = 1;
for (let i = 1; i <= n; i++)
    if (i % 2 == 0) {
        alturaArvore += 1;
    } else {
        alturaArvore *= 2;
    }
return alturaArvore;

}

-1

// essa vai dar certinho

function alturaArvoreUtopica(n) {

var estacoes = []
var arvore = 1
if (n == 0) {
    return arvore;
} if (n == 1) {
    return arvore + 1;
}
for (var i = 0; i <= n; i++) {

    if (i % 2 == 1 && i > 0) {
        estacoes.push("inverno")
    } if (i % 2 == 0 && i !== 0) {
        estacoes.push("verao")
    }
}
for (var c = 0; c <= estacoes.length; c++) {

    if (estacoes[c] === "inverno") {
        arvore = arvore * 2;
    }
    if (estacoes[c] === "verao") {
        arvore = arvore + 1;
    }
}
return arvore

}

-1

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

Assuming:

The tree starts with 1 meter; No matter what the season, its order of occurrence is and that each cycle is a season. And that growth occurs in such a way that: Summer: tree size increases by 1; autumn: size is not changed; Winter: the size is not changed; Spring: size is multiplied by 2.

  • Explain your response Provide data where anyone can read and understand your answer.

Browser other questions tagged

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