0% found this document useful (0 votes)
47 views17 pages

Assign 7

Uploaded by

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

Assign 7

Uploaded by

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

ASSIGNMENT-7

1. Write JavaScript code to change the contents of a paragraph dynamically by inserting


JavaScript in <head>.
<!DOCTYPE html>
<html>
<head>
<script>
window.onload = function() {
document.getElementById('para').textContent = 'Content changed!';
};
</script>
</head>
<body>
<p id="para">Original Content</p>
</body>
</html>

Output-

2. Write JavaScript code to change the contents of a paragraph dynamically by inserting


JavaScript in <body>.
<!DOCTYPE html>
<html>
<body>
<p id="para">Original Content</p>
<script>
document.getElementById('para').textContent = 'Content changed!';
</script>
</body>
</html>
Output-
3. Write JavaScript code to change the contents of a heading dynamically by using external
.js file.
Html-
<!DOCTYPE html>
<html>
<head><script src="script.js" defer></script></head>
<body>
<h1 id="heading">Original Heading</h1>
</body>
</html>

Script.js-
document.getElementById('heading').textContent = 'Updated Heading';
Output-

4. Write JavaScript code to display the sum of two numbers using.


a. document.write()
b. window.alert()
c. innerHTML
d. console.log()
<!DOCTYPE html>
<html>
<head><script >
let a = 5, b = 3, sum = a + b;
document.write(sum);
window.alert(sum);
document.getElementById('output').innerHTML = sum;
console.log(sum);

</script></head>
<body>

</body>
</html>
Output-

5. Write JavaScript code to change the contents of a paragraph to sum of three numbers.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p id="para"></p>
<script>
let sum = 5 + 3 + 2;
document.getElementById('para').textContent = `Sum: ${sum}`;
</script>
</body>
</html>
Output-

6. Write JavaScript code to find the largest among three numbers.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let a = 10, b = 20, c = 15;
let largest = Math.max(a, b, c);
console.log(largest);
</script>
</body>
</html>
Output-

7. Write JavaScript code to find if a number is prime or not.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let num = 7, isPrime = true;
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) isPrime = false;
}
console.log(isPrime ? 'Prime' : 'Not Prime');
</script>
</body>
</html>
Output-

8. Write JavaScript code to add 2 numeric strings, add one number and one numeric strings,
add one numeric string and one number, multiply 2 numeric strings (100,10), subtract 2
numeric strings (100,10), divide 2 numeric strings (100,10) .
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
console.log(+'100' + +'10');
console.log(+'100' + 10);
console.log('100' * '10');
console.log('100' - '10');
console.log('100' / '10');

</script>
</body>
</html>

Output-
9. Write JavaScript code to convert Celsius to Fahrenheit by passing variable to function.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function celsiusToFahrenheit(c) {
return (c * 9) / 5 + 32;
}
console.log(celsiusToFahrenheit(25));

</script>
</body>
</html>

Output-

10. Write JavaScript code to find factorial of a number by passing variable to function.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function factorial(n) {
return n <= 1 ? 1 : n * factorial(n - 1);
}
console.log(factorial(5));
</script>
</body>
</html>

Output

11. Write a JavaScript function that 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>Document</title>
</head>
<body>
<script>
function reverseNumber(n) {
return Number(n.toString().split('').reverse().join(''));
}
console.log(reverseNumber(12345));

</script>
</body>
</html>

Output
12. Write a JavaScript function that accepts a number as a parameter and check the
number is prime or not.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function isPrime(n) {
for (let i = 2; i <= Math.sqrt(n); i++) if (n % i === 0) return false;
return n > 1;
}
console.log(isPrime(7));

</script>
</body>
</html>

Output

13. Write a JavaScript function which accepts an argument and returns the type.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function getType(arg) {
return typeof arg;
}
console.log(getType(123));

</script>
</body>
</html>

Output

14. Write a JavaScript function to add 3 numbers by passing parameters.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function add(a, b, c) {
return a + b + c;
}
console.log(add(5, 3, 2));

</script>
</body>
</html>

Output
15. Write a JavaScript function to display the weekday by passing the weekday number
as input.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function getWeekday(n) {
let days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
return days[n];
}
console.log(getWeekday(2));

</script>
</body>
</html>

Output

16. Write a JavaScript function to get the answer through prompt for sum of two numbers
and check if the answer is correct or not. Display an alert message accordingly.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let a = 5, b = 3, sum = a + b;
let userAns = prompt(`What is ${a} + ${b}?`);
alert(userAns == sum ? 'Correct!' : 'Wrong!');

</script>
</body>
</html>

Output

17. Write java script code to take two integer inputs from user and a valid arithmetic
operator. Perform the operation and display the result back to the user.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let a = Number(prompt('Enter first number'));
let b = Number(prompt('Enter second number'));
let op = prompt('Enter operator (+, -, *, /)');
let result = eval(`${a} ${op} ${b}`);
alert(`Result: ${result}`);

</script>
</body>
</html>

Output
18. Write a program using JavaScript to receive four numbers from text boxes and display
biggest, smallest, sum and average of the numbers.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input id="nums" placeholder="Enter 4 numbers comma-separated">
<button onclick="analyze()">Submit</button>
<p id="result"></p>
<script>
function analyze() {
let nums = document.getElementById('nums').value.split(',').map(Number);
let sum = nums.reduce((a, b) => a + b);
document.getElementById('result').textContent = `
Biggest: ${Math.max(...nums)}, Smallest: ${Math.min(...nums)},
Sum: ${sum}, Average: ${sum / nums.length}`;
}
</script>

</body>
</html>

Output

19. Write a program using JavaScript to design a calculator using form.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator</title>

</head>
<body>
<script>
function add(num1, num2) {
return num1 + num2;
}
function subtract(num1, num2) {
return num1 - num2;
}
function multiply(num1, num2) {
return num1 * num2;
}
function divide(num1, num2) {
if(num2 === 0) return undefined;
return num1 / num2;
}
let num1 = 16;
let num2 = 4;

let operation = "*";


let result;
if (operation === "+") {
result = add(num1, num2);
} else if (operation === "-") {
result = subtract(num1, num2);
} else if (operation === "*") {
result = multiply(num1, num2);
} else if (operation === "/") {
result = divide(num1, num2);
} else {
result = "Invalid operation";
}
console.log("The Result of this operation is : " + result);
</script>
</body>
</html>

Output

20. Write java script code to check a given no is prime or not using function. The output should
look like as follows:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Prime Number Checker</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin-top: 50px;
}
h2{
color: #333;
position: relative;
display: inline-block;
margin-bottom: 20px;
}
h2::after {
content: "";
display: block;
width: 100%;
height: 2px;
background-color: red;
margin-top: 10px;
}
input, button {
margin: 10px;
padding: 10px;
font-size: 16px;
}
.result {
margin-top: 20px;
font-weight: bold;
color: #007bff;
}
</style>
</head>
<body>
<h2>To check wheather a given number is Prime or not</h2>
<form onsubmit="checkPrime(); return false;">
<input type="number" id="number" placeholder="Enter a number" required><br>
<button type="submit">Check</button>
</form>
<p class="result" id="result"></p>

<script>
function isPrime(num) {
if (num < 2) return false;
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) return false;
}
return true;
}

function checkPrime() {
const num = parseInt(document.getElementById('number').value);
const resultElement = document.getElementById('result');
if (isNaN(num)) {
resultElement.textContent = "Please enter a valid number.";
return;
}
const result = isPrime(num)
? `${num} is a prime number.`
: `${num} is not a prime number.`;
resultElement.textContent = result;
}
</script>
</body>
</html>

Output-

You might also like