The document contains a JavaScript code that initializes an array with 10 elements and prompts the user to enter names for each element. If the user provides no input, a default message is stored instead. Finally, it displays the entered names in a structured list format on the webpage.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
2 views1 page
Array
The document contains a JavaScript code that initializes an array with 10 elements and prompts the user to enter names for each element. If the user provides no input, a default message is stored instead. Finally, it displays the entered names in a structured list format on the webpage.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1
<html>
<head> <title>To create an array of 10 elements and display its contents</title> </head> <body> <script type="text/javascript"> var a = new Array(10); // Initialize an array with 10 elements
// Asking for input only once
alert("Please enter 10 names.");
// Collecting names through prompt
for (let i = 0; i < a.length; i++) { let name = prompt("Enter value for element " + (i + 1) + ":");
// Check if the input is empty
if (name === null || name.trim() === "") { a[i] = "No input provided"; // default message for empty input } else { a[i] = name.trim(); // Store the entered value } }
// Displaying values with more structured output
document.write("<h3><b>Given values are:</b></h3>"); document.write("<ul>"); for (let i = 0; i < a.length; i++) { document.write("<li>" + a[i] + "</li>"); } document.write("</ul>"); </script> </body> </html>