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

168
Views
¿Hay alguna manera de mostrar el contenido de una matriz como una lista ordenada pero dentro de un cuadro de alerta?

Estoy escribiendo un programa que solicita información a través de cuadros de texto y luego solicita más elementos a través de indicaciones. Estoy usando el método .push para agregar las entradas a la matriz vacía que tengo. Al final del programa, mostrará los elementos dentro de un cuadro de alerta, pero los elementos deben estar en orden alfabético y mostrarse como una lista ordenada. Sé que esto se puede hacer en una matriz usando el método .map, pero todos los ejemplos que veo muestran esto en la página y no dentro de una alerta.

Aquí está el código que estoy usando.

 let list = []; function groceryList() { text_one = document.getElementById('text1').value; text_two = document.getElementById('text2').value; text_three = document.getElementById('text3').value; list.push(text_one, text_two, text_three); add = prompt("Enter another item on your grocery list: (Enter 0 to finish)"); while (add !== 0) { list.push(add); add = prompt("Enter another item on your grocery list: (Enter 0 to finish)"); if (add == 0) { break; } } list.sort(); alert(list); }
 <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>Grocery List</title> </head> <body> <script type="text/javascript" src="my_list.js" charset="utf-8"></script> <p>Please use the form below to add 3 items to the grocery list.</p> <form onsubmit="groceryList()"> <input type="text" id="text1"> <input type="text" id="text2"> <input type="text" id="text3"> <br> <br> <input type="submit" value="Add to the grocery list"> </form> </body> </html>

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

0

Prueba esto:

 ... list.sort(); let orderedList = list.reduce((list,item, index)=>{ return `${list}${index+1}. ${item}\n`},""); alert(orderedList); ...
about 4 years ago · Juan Pablo Isaza Report

0

 <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>Grocery List</title> </head> <body> <script type="text/javascript" charset="utf-8"> let list = []; function groceryList() { text_one = document.getElementById('text1').value; text_two = document.getElementById('text2').value; text_three = document.getElementById('text3').value; list.push(text_one, text_two, text_three); add = prompt("Enter another item on your grocery list: (Enter 0 to finish)"); while (add !== 0) { list.push(add); add = prompt("Enter another item on your grocery list: (Enter 0 to finish)"); if (add == 0) { break; } } list.sort(); alert(list.join('\n')); } </script> <p>Please use the form below to add 3 items to the grocery list.</p> <form onsubmit="groceryList()"> <input type="text" id="text1"> <input type="text" id="text2"> <input type="text" id="text3"> <br> <br> <input type="submit" value="Add to the grocery list"> </form> </body> </html>

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!