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))
"Spring" and "summer" represent how many months? Your question is a little vague. Could you provide a little more detail, please?
– Luiz Felipe
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)
– user139817
In autumn and winter the tree does not grow?
– Luiz Felipe
No, only in the spring and summer cycles
– user139817
The height receives + 1 in summer and in spring the height is multiplied by 2?
– Luiz Felipe
Exactly that Luiz
– user139817