DEV Community

Cristian Fernando
Cristian Fernando

Posted on

1 1

Advent.js🎅🏼| #4: ¡Es hora de poner la navidad en casa!

¡Es hora de poner la navidad en casa!

¡Es hora de poner el árbol de navidad en casa! 🎄

Para ello vamos a crear una función que recibe la altura del árbol, que será un entero positivo del 1 a, como máximo, 100.

Si le pasamos el argumento 5, se pintaría esto:

____*____
___***___
__*****__
_*******_
*********
____#____
____#____
Enter fullscreen mode Exit fullscreen mode

Creamos un triángulo de asteríscos * con la altura proporcionada y, a los lados, usamos el guión bajo _ para los espacios. Es muy importante que nuestro árbol siempre tenga la misma longitud por cada lado.
Todos los árboles, por pequeños o grandes que sean, tienen un tronco de dos líneas de #.

Otro ejemplo con un árbol de altura 3:

__*__
_***_
*****
__#__
__#__
Enter fullscreen mode Exit fullscreen mode

Ten en cuenta que el árbol es un string y necesitas los saltos de línea \n para cada línea para que se forme bien el árbol.

Completa el reto!


Mi solución:


Puedes seguir a @midudev y estar pendiente de los retos de Advent.js

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

Top comments (2)

Collapse
 
kylety1990 profile image
kylety1990

function createTree(number){
let tree = '';
let counter = 1;
const objTree = {
background : '_',
branch : '*',
trunk : '#',
}
const {background, branch, trunk} = objTree;
const data = Array.from(Array(number).keys()).reverse();

data.map(el=>{
     tree += file(branch, background, el,  counter);
     counter += 2;
})
Enter fullscreen mode Exit fullscreen mode

tree += file(trunk, background, data.length -1 , 1);
tree += file(trunk, background, data.length -1, 1);
return tree
}

function file(branch, background, number, counter){
return ${background.repeat(number)}${branch.repeat(counter)}${background.repeat(number)}\n

}

console.log(createTree(10))

Collapse
 
alexjamesmx profile image
Alejandro Santiago
export default function createXmasTree(height) {
  let string = ""
  console.log(height)
  for(let i = 0; i < height; i++){

    for(let x= 0 ; x < height-i-1 ; x ++){
      string += "_"
    }
    for(let x= 0 ; x < ((i+1)*2)-1 ; x ++){
      string += "*"
    } 
     for(let x= 0 ; x < height-i-1 ; x ++){
      string += "_"
    }

  string += "\n"
  }
  for(let i = 0 ; i < 2; i++){
     if(i==1) string += "\n"
    for(let i = 0 ; i < height-1;i++){
      string += "_"
    }
      string += "\#"
       for(let i = 0 ; i < height-1;i++){
      string += "_"
    }

  }
console.log(string)
  return string
  }
Enter fullscreen mode Exit fullscreen mode

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay