For & If exercise (doubt about error obtained)

Asked

Viewed 63 times

1

I was looking for an answer to an exercise I’m doing and, the platform where I’m studying, returned with error what I wrote.

The exercise was:

Utopian trees grow in a particular way in two cycles:

  • Each spring doubles its size
  • Each summer grow a meter

If Laura plants a one meter utopian tree in late autumn, what would be her 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 the next spring)

Write the function alturaArvoreUtopica, that uses an amount of growth cycles, and returns the resulting height of Laura’s tree.

My first solution was:

    function alturaArvoreUtopica(ciclos){
    var tamanhoDaArvore = 1;

    for(var i = 1; i <= ciclos; i++){
        if (i % 2 ===0) tamanhoDaArvore += 1;
    } else {
        tamanhoDaArvore *=2;
    }
    return tamanhoDaArvore
}
console.log(alturaArvoreUtopica);

However, when sending the exercise through the platform, it did not pass (I should have printed the error). I came in here to get something on and found here. exactly the same exercise, with the solution below (and that went through the platform):

function alturaArvoreUtopica(ciclos){
    var tamanhoDaArvore = 1;

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

console.log(alturaArvoreUtopica(4));

Doubt: I can not understand why what I wrote did not pass and, the second solution found here, passed. If anyone can help me with this understanding, it would help me a lot!

  • It is because you have a syntax error. The else is after the for (note the lock of keys before the word else) and not of if

  • Function alturaArvoreUtopica(cycles){var sizeDaArvore = 1;for (var i = 1; i <= cycles; i++) {if (i % 2 ===0){ sizeDaArvore += 1; }Else{ sizeDaArvore *= 2;}}Return sizeDaArvore;}console.log(highArvoretopica(4));;

  • The code above runs giving 7 as reultado. It is in a line only pq here can only be so, but works well

  • Great! Thank you, @Leocaracciolo!

  • And no problem @LINQ!

2 answers

2


Only to expand knowledge.

Inside the for use a shortcut to the if statement, Ternary Conditional Operator.

function alturaArvoreUtopica(ciclos){
	var tamanhoDaArvore = 1;
	for (var i = 1; i <= ciclos; i++) {
		(i % 2 ===0)? tamanhoDaArvore += 1: tamanhoDaArvore *= 2;
	}
	return tamanhoDaArvore;
}
console.log(alturaArvoreUtopica(4));

In addition to deleting keys { } the if else also went into space. That way you wouldn’t mess with the keys.

It is also important to observe the response of JULIANO LANDIM regarding the parameter to be passed to the function

1

Hello, you are not passing the parameter value to call the function in console.log,
you are calling console.log(alturaArvoreUtopica); instead of console.log(alturaArvoreUtopica(4));

Moreover has error in opening and closing if keys:

for(var i = 1; i <= ciclos; i++){ if (i % 2 ===0){ tamanhoDaArvore += 1; }else{ tamanhoDaArvore *=2; } }

  • Juliano, so if I had done the if inside the keys, Else would run normal and pass the code... and correct the console call.log.

Browser other questions tagged

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