Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

304
Views
La cadena devuelve una línea vacía después de cada entrada

He estado tratando de resolver esto por un tiempo, pero se me acabaron las posibles soluciones. Esto es para una tarea escolar que está destinada a crear un "triángulo" basado en el número ingresado (así que si pongo 3, la primera línea tendrá 1 asterisco, la segunda tendrá 2 y la tercera 3). El código funciona bien, pero aparece una línea vacía adicional debido a "\n" y no estoy seguro de cómo eliminarlo. Mi código:

 function drawTriangle(size) { let string = ""; //declares a variable string for (let i = 1; i <= size; i++) { //loop continues upto size for (let j = 0; j < i; j++) { //loop starts from 0 and ends at i string += "*"; //* is added to string } string += "\n"; //when 1 row is finished, next line is printed on new line } console.log(string); //outputs the string to the console }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Según el comentario Bergi's , cree una variable de matriz local y agregue un salto de línea al final usando .join("/n") .

Demostración en vivo :

 function drawTriangle(size) { let str = ''; //declares a variable string const arr = []; for (let i = 1; i <= size; i++) { //loop continues upto size for (let j = 0; j < i; j++) { //loop starts from 0 and ends at i str += "*"; //* is added to string } arr.push(str); str = ''; } return arr }; const result = drawTriangle(3).join("\n"); console.log(result);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!