You are on page 1of 2

1. Develop any javaScript to implement functions.

<!DOCTYPE HTML>
<html>
<head>
<title>Function Demo</title>
<script type="text/javascript">
function my_fun()
{
document.write("This statement is within the function");
}
</script>
</strong>
<body>
<center>
<script type="text/javascript">
document.write("This statement is before a function call ");
document.write("<br>");
my_fun();
</script>
</center>
</body>
</html>

2. Write a JavaScript to call function from Html.


<!DOCTYPE html>
<html>
<head>
<title>Call JavaScript from HTML</title>
<script>
// Define a JavaScript function
function greet() {
alert("Hello, world!");
}
</script>
</head>
<body>
<!-- Call the JavaScript function when the button is clicked -->
<button onclick="greet()">Click me to greet</button>
</body>
</html>
3. Write a JavaScript function that checks wether a passed string is palindrom or not.

<script>
function isPalindrome(str) {
// Remove non-alphanumeric characters and convert to lowercase
str = str.replace(/[^a-zA-Z0-9]/g, "").toLowerCase();

// Compare the reversed string with the original


return str === str.split("").reverse().join("");
}

document.write(isPalindrome("racecar")); // true
document.write(isPalindrome("hello")); // false
document.write(isPalindrome("A man, a plan, a canal, Panama")); // true
</script>

4. Write JavaScript function to generate Fibonacci Series till user defind limit.

<script>
function generateFibonacciSeries(limit) {
if (limit <= 0) {
return [];
} else if (limit === 1) {
return [0];
}

const series = [0, 1];


for (let i = 2; series[i - 1] + series[i - 2] <= limit; i++) {
series.push(series[i - 1] + series[i - 2]);
}

return series;
}

// Example: Generate Fibonacci series up to a limit of 50


const limit = 50;
const fibonacciSeries = generateFibonacciSeries(limit);
document.write (fibonacciSeries); // Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

</script>

You might also like