Doubt in the for and if

Asked

Viewed 343 times

-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)
  • 1

    What have you tried? post in question...

  • 2

    when the i is odd fold height, when pair grows 1, you can know if the i is even or not using module operator % ex: if (i % 2 === 0) // é par ... begin with the i to 1

  • @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.

  • 1

    must begin with the i to 1, and be up to i <= ciclos

  • @Vik, now it worked, I changed to i <=cycles.

  • @Vik creates an answer to be accepted and finalize the question

Show 1 more comment

2 answers

1

That sequence of numbers (1, 2, 3, 6, 7, 14, 15) has the pattern of 'double', 'plus 1', 'double', 'plus 1'...

Then we will have to use a variable that alternates its state but has only two possible states...

Take the value of the counter i and whether it’s even number or not, serves the problem. The modulus operator % returns the rest of the division, so if we use numero % 2 in whole numbers, we will always have the result of 0 or 1 (even or odd).

function alturaArvoreUtopica(ciclos) {

    let tamanhoArvore = 1;

    for (let i = 1; i <= ciclos; i++) {

        if (i % 2 === 0) tamanhoArvore += 1
        else tamanhoArvore *= 2;
    }

    return tamanhoArvore;
}

console.log(alturaArvoreUtopica(4));       

We can change the if else by the ternary conditional operator, since both conditions assign a value to the variable.

tamanhoArvore += i % 2 ? tamanhoArvore : 1;

-1

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

}

Browser other questions tagged

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