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

Assignment 3.2

The document contains code to create a form with input field and buttons to perform mathematical operations on a number entered in the input field. Arrow functions are used to find the factorial, check if prime, find sum of digits, find product of digits of a number, and clear the form. The number input is retrieved and operations are performed on it before displaying the result.

Uploaded by

Vandana Garia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Assignment 3.2

The document contains code to create a form with input field and buttons to perform mathematical operations on a number entered in the input field. Arrow functions are used to find the factorial, check if prime, find sum of digits, find product of digits of a number, and clear the form. The number input is retrieved and operations are performed on it before displaying the result.

Uploaded by

Vandana Garia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Qn 1 -: Complete the task using Arrow Functions

Code -:

<form>

Enter a number: <input type="number" id="number">

<br>

<button onclick="findFactorial()">Find Factorial</button>

<button onclick="checkPrime()">Check Prime</button>

<button onclick="findSumOfDigits()">Find Sum of Digits</button>

<button onclick="findProductOfDigits()">Find Product of Digits</button>

<button onclick="clearForm()">Clear</button>

<br>

Result: <input type="text" id="result" disabled>

</form>

<script>

// Arrow Function for finding the factorial of a number

const findFactorial = () => {

const number = parseInt(document.getElementById("number").value);

let result = 1;

for (let i = 2; i <= number; i++) {

result *= i;

document.getElementById("result").value = result;

};
// Arrow Function for checking if a number is prime

const checkPrime = () => {

const number = parseInt(document.getElementById("number").value);

let isPrime = true;

if (number === 1 || number === 0) {

isPrime = false;

for (let i = 2; i <= Math.sqrt(number); i++) {

if (number % i === 0) {

isPrime = false;

break;

document.getElementById("result").value = isPrime ? `${number} is a prime number` : `${number} is


not a prime number`;

};

// Arrow Function for finding the sum of digits of a number

const findSumOfDigits = () => {

const number = parseInt(document.getElementById("number").value);

let numberString = number.toString();

let sum = 0;

for (let i = 0; i < numberString.length; i++) {

sum += parseInt(numberString.charAt(i));

document.getElementById("result").value = sum;
};

// Arrow Function for finding the product of digits of a number

const findProductOfDigits = () => {

const number = parseInt(document.getElementById("number").value);

let numberString = number.toString();

let product = 1;

for (let i = 0; i < numberString.length; i++) {

product *= parseInt(numberString.charAt(i));

document.getElementById("result").value = product;

};

// Arrow Function for clearing the form

const clearForm = () => {

document.getElementById("number").value = "";

document.getElementById("result").value = "";

};

</script>

Output -:

You might also like