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

206
Views
función para devolver algo x cantidad de veces, especificado por un parámetro?

Tengo una función que devuelve un elemento html como addSpan(times) , ¿hay alguna forma de devolver este elemento tantas veces como se especifica en los items del parámetro?

¡vainilla js es bienvenida!

 function addSpan(times){ const span = `<span>This is a span</span>` return span } $("body").append( addSpan(2) ) //is supposed to add 2 spans
 <!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery-3.6.0.js"></script> </head> <body> </body> </html>

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Utilice la función Array.map().

 function addSpan(times){ const spans = [...Array(times)].map(val => "<span>This is a span</span>").join('') return spans }
about 4 years ago · Juan Pablo Isaza Report

0

rendimiento de uso

 function* addSpan(times){ for(var i=0; i < times; i++){ const span = document.createElement('span'); span.innerText = "Some text" ; yield span; } } for (let element of addSpan(2)) { $("body").append(element) }
 <!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery-3.6.0.js"></script> </head> <body> </body> </html>

about 4 years ago · Juan Pablo Isaza Report

0

Sí. Posible.

Como esto:

 function addSpan(count) { let result = ""; for (var i = 0; i < count; i++) { result += "<span>This is a span</span>"; } return 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!