loader image
Teachics

JavaScript program to read values using prompt popup box

Write a JavaScript Program to create an Array and read values using the Prompt popup box and display the sum of elements in an Alert Box.

<html>

<body>
    <script>
        var n = window.prompt("Enter size of the array");
        var arr = new Array();
        var sum = 0;
        for (let i = 1; i <= n; i++) {
            arr[i] = parseInt(window.prompt("Enter element " + i));
            sum = sum + arr[i];
        }
        alert("Sum =" + sum);
    </script>

</body>

</html>
Output
prompt 1 js
prompt 2 js
prompt 3 js
prompt 4 js
prompt 5 js
prompt 6 js
prompt 7 js
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments