3 Print
3 Print
3 Print
<title>Button Actions</title>
<body>
<button id="prime">Prime</button>
<button id="even-odd">Even/Odd</button>
<button id="prime-factors">Prime Factors</button>
<button id="palindrome">Palindrome</button>
<script>
function isPrime(num) {
if (num <= 1) {
return false;
}
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
return false;
}return true;}
function primeFactors(num) {
let factors = [];
for (let i = 2; i <= num; i++) {
while (num % i === 0) {
factors.push(i);
num /= i;
}} return factors;}
function isEvenOrOdd(num) {
return num % 2 === 0 ? "Even" : "Odd";}
function isPalindrome(str) {
return str === str.split("").reverse().join("");}
document.getElementById("prime").addEventListener("click", function() {
let num = parseInt(prompt("Enter a number:"));
if (isPrime(num)) {
alert(num + " is prime.");
} else {
alert(num + " is not prime.");
}
});
document.getElementById("even-odd").addEventListener("click", function() {
let num = parseInt(prompt("Enter a number:"));
alert(num + " is " + isEvenOrOdd(num) + ".");
});
document.getElementById("prime-factors").addEventListener("click", function() {
let num = parseInt(prompt("Enter a number:"));
alert("The prime factors of " + num + " are " + primeFactors(num).join(", ") + ".");
});
document.getElementById("palindrome").addEventListener("click", function() {
let str = prompt("Enter a string:");
if (isPalindrome(str)) {
alert(str + " is a palindrome.");
} else {
alert(str + " is not a palindrome.");} });
</script>
9.function validateName(name) { let name = "John Doe";
if (name.length < 2 || name.length > 20) { let email =
return false;} "johndoe@example.com";
if (!/^[A-Za-z ]+$/.test(name)) { let password = "Abcdefg1";
return false;} if (validateName(name)) {
return true;} console.log("Name is valid.");
function validateEmail(email) { } else {
if (!/\S+@\S+\.\S+/.test(email)) { console.log("Name is not valid.");}
return false;} if (validateEmail(email)) {
return true;} console.log("Email is valid.");
function validatePassword(password) { } else {
if (password.length < 8) { console.log("Email is not valid.");}
return false;} if (validatePassword(password)) {
if (!/(?=.[a-z])(?=.[A-Z])(?=.*\d)/.test(password)) { console.log("Password is valid.");
return false;} } else {
return true;} console.log("Password is not
valid.");}
7. <title>Squares of numbers</title>
<body>
<ul>
<li><a href="#" onclick="showSquare(1)">1</a></li>
<li><a href="#" onclick="showSquare(2)">2</a></li>
<li><a href="#" onclick="showSquare(3)">3</a></li>
<li><a href="#" onclick="showSquare(4)">4</a></li>
<li><a href="#" onclick="showSquare(5)">5</a></li>
<li><a href="#" onclick="showSquare(6)">6</a></li>
<li><a href="#" onclick="showSquare(7)">7</a></li>
<li><a href="#" onclick="showSquare(8)">8</a></li>
<li><a href="#" onclick="showSquare(9)">9</a></li>
<li><a href="#" onclick="showSquare(10)">10</a></li>
</ul>
<p id="result"></p>
<script>
function showSquare(num) {
let square = num * num;
document.querySelector("#result").textContent = "The square of " + num + " is " + square + "."; }
</script>
2 <title>Reverse Case</title>
<script>
function reverseCase() {
var input = document.getElementById("inputText").value;
var output = "";
for (var i = 0; i < input.length; i++) {
var char = input.charAt(i);
if (char === char.toUpperCase()) {
output += char.toLowerCase();
} else {
output += char.toUpperCase();}}
document.getElementById("outputText").value = output;} }
</script>
<body>
<label for="inputText">Input:</label>
<input type="text" id="inputText"><br><br>
<label for="outputText">Output:</label>
<input type="text" id="outputText"><br><br>
<input type="button" value="Reverse Case" onclick="reverseCase()">
</body>