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

Javascript Program

Uploaded by

Rio Jay Magalona
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Javascript Program

Uploaded by

Rio Jay Magalona
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Sample program.

<!DOCTYPE html>
<html lang="en">
<head>

</head>
<body>

<script>
function calculateSum() {
// Get the input values
var num1 = parseInt(document.getElementById('num1').value);
var num2 = parseInt(document.getElementById('num2').value);

// Call the function to calculate the sum


var sum = addNumbers(num1, num2);

// Display the result


document.getElementById('result').innerHTML = "The sum is: " + sum;
}

function addNumbers(a, b) {
return a + b;
}

</script>

<h2>Sum Calculator</h2>
<form id="sumForm">
<label for="num1">Enter first number:</label>
<input type="number" id="num1" name="num1" required><br><br>

<label for="num2">Enter second number:</label>


<input type="number" id="num2" name="num2" required><br><br>

<button type="button" onclick="calculateSum()">Calculate Sum</button>


</form>

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

<script src="script.js"></script>
</body>
</html>
============================================SECOND EXAMPLE:
<!DOCTYPE html>
<html lang="en">
<head>

</head>
<body>

<script>
function calculateSum() {
// Get the input array values
var inputArray = document.getElementById('arrayValues').value;

// Convert the input string into an array


var array = inputArray.split(",").map(function(item) {
return parseInt(item.trim(), 10); // Convert each item to an integer
});

// Call the function to calculate the sum


var sum = sumArray(array);

// Display the result


document.getElementById('result').innerHTML = "The sum is: " + sum;
}

function sumArray(arr) {
var sum = 0;
for (var i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}

</script>

<h2>Array Sum Calculator</h2>


<form id="arrayForm">
<label for="arrayValues">Enter array values (comma-separated):</label>
<input type="text" id="arrayValues" name="arrayValues" required><br><br>

<button type="button" onclick="calculateSum()">Calculate Sum</button>


</form>

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

<script src="script.js"></script>
</body>
</html>
===========================================
same as before regarding your 1st activities (please your answer in the gdrive) -
program and output must be present in the MSWORD.
[email protected] - invite me as editor. thanks po

1. Sum of Array Elements: Write a function that takes an array of numbers as input
and returns the sum of all the elements.
2. Reverse String: Write a function that takes a string as input and returns the
reverse of that string.
3. Find Maximum Number: Write a function that takes an array of numbers as input
and returns the maximum number in the array.
4. Factorial: Write a function that calculates the factorial of a given number
using recursion.
5. Check Prime Number: Write a function that checks whether a given number is prime
or not.
6. Generate Random Number: Use the Math.random() method to generate a random number
between a specified range.
7. Count Vowels in a String: Write a function that counts the number of vowels in a
given string.
8. Convert Celsius to Fahrenheit: Write a function that converts temperature from
Celsius to Fahrenheit.
9. Get Current Date and Time: Use the Date object to get the current date and time.
10. Calculate Age: Write a function that calculates the age of a person based on
their birthdate.

You might also like