Untitled
unknown
plain_text
16 days ago
1.7 kB
2
Indexable
1a. Source Code: <!DOCTYPE html> <html> <head> <title>Dynamic Input Experiment</title> <script> // Log "Hello, World!" to the console console.log("Hello, World!"); // Display "Hello, World!" on the webpage document.addEventListener("DOMContentLoaded", function() { document.body.innerHTML += "<h1>Hello, World!</h1>"; }); // Read numbers from user input let num1 = parseFloat(prompt("Enter the first number:")); let num2 = parseFloat(prompt("Enter the second number:")); // Validate input and calculate sum if (!isNaN(num1) && !isNaN(num2)) { let sum = num1 + num2; alert("The sum is: " + sum); } else { alert("Invalid input! Please enter numbers."); } </script> </head> <body> </body> </html> 1b. Source Code: const prompt = require('prompt-sync')(); // Prompt the user to enter 5 city names, separated by commas let input = prompt("Enter 5 cities separated by commas: "); let cities = input.split(',').map(city => city.trim()); console.log("Initial cities:", cities); // Log the total number of cities console.log("Total number of cities:", cities.length); // Add a new city at the end let newCity = prompt("Enter a city to add to the end: "); cities.push(newCity); console.log("Cities after adding a new one:", cities); // Remove the first city //console.log("Removing the first city:", cities[0]); // Log the first city before removal cities.shift(); console.log("Cities after removing the first one:", cities); // Find and log the index of a specific city let searchCity = prompt("Enter a city to find its index: "); let cityIndex = cities.indexOf(searchCity); console.log("Index of", searchCity + ":", cityIndex !== -1 ? cityIndex : "City notfound");
Editor is loading...
Leave a Comment