0% found this document useful (0 votes)
2 views

Assignment 3

The document contains two HTML programs. The first program calculates the sum and average of an array of numbers inputted by the user, while the second program finds the position of the leftmost vowel in a string and reverses a given number. Both programs utilize JavaScript for functionality and display results on the webpage.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment 3

The document contains two HTML programs. The first program calculates the sum and average of an array of numbers inputted by the user, while the second program finds the position of the leftmost vowel in a string and reverses a given number. Both programs utilize JavaScript for functionality and display results on the webpage.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment-3

1. Write an HTML program to calculate the sum of an array.

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Array Sum and Average</title>

<script>

function calculate() {

var inputArray = document.getElementById("inputArray").value;

var numbers = inputArray.split(",").map(Number);

var sum = 0;

for (var i = 0; i < numbers.length; i++) {

sum += numbers[i];

var average = sum / numbers.length;

document.getElementById("result").innerHTML = "Sum: " + sum +


"<br>Average: " + average;

</script>

</head>

<body>

<h2>Enter elements separated by commas:</h2>

<textarea id="inputArray" rows="4" cols="50"></textarea><br><br>

<button onclick="calculate()">Show Result</button><br><br>

<div id="result"></div>

</body>

</html>
Output:

2. Write an HTML code to find the left most vowel and reverse a number.

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>String and Number Manipulation</title>

<script>

function findLeftMostVowel(str) {

var vowels = ['a', 'e', 'i', 'o', 'u'];

str = str.toLowerCase();

for (var i = 0; i < str.length; i++) {

if (vowels.includes(str[i])) {

return i + 1;

}
return -1;

function reverseNumber(num) {

var reversed = parseInt(num.toString().split('').reverse().join(''));

return reversed;

function testFunctions() {

var inputStr = document.getElementById("inputString").value;

var inputNum = parseInt(document.getElementById("inputNumber").value);

var leftMostVowelPosition = findLeftMostVowel(inputStr);

var reversedNumber = reverseNumber(inputNum);

document.getElementById("outputLeftMostVowel").innerHTML = "Left Most Vowel Position: " +


leftMostVowelPosition;

document.getElementById("outputReversedNumber").innerHTML = "Reversed Number: " +


reversedNumber;

</script>

</head>

<body>

<h2>Problem a: Find Left Most Vowel</h2>

<input type="text" id="inputString" placeholder="Enter a string">

<button onclick="testFunctions()">Find Left Most Vowel</button>

<p id="outputLeftMostVowel"></p>

<h2>Problem b: Reverse Number</h2>

<input type="number" id="inputNumber" placeholder="Enter a number">

<button onclick="testFunctions()">Reverse Number</button>

<p id="outputReversedNumber"></p>
</body>

</html>

Output:

You might also like