Javascript File
Javascript File
Name - ………………………………..
Class - ………………………………..
Subject- ………………………………..
Session ………………………………..
………………………………..
CERTIFICATE
Output
Q4: Write a JavaScript Program by using function print( ) first five
natural numbers by using while loop.
Ans:
Output:
Q5: Write a JavaScript Program by using function odd( ) used to display
odd numbers upto the specified limit entered by the user.
Ans:
Output:
Q6: Write a JavaScript Program by using function to find the sum of
positive numbers the while loop runs infinitely loop terminates only when user
enters a negative number.
Ans:
Q7: Write a JavaScript Program by using function table( ) used to display
table of a number entered by the user.
Ans:
Output:
Q8: Write a JavaScript Program by using function leap( ) used to check
the inputted year is a leap year or not. A year is a leap year if the following
conditions are satisfied:
The year is a multiple of 400.
The year is a multiple of 4 and not a multiple of 100.
Ans:
Output:
Q9: Write a JavaScript Program by using function used to convert first
letter of the entered string into capital letter.
Ans:
Output:
Q10 : Write a program to accept a number from the user. Based on the choice ‘S’ or ‘C’
entered by the user, calculate and display:
i. Square of the number
ii. Cube of the number
Ans:
Output:
Javascript File questions
Q11: Declare a function convert(a,b) in JavaScript to accept two strings arguments. The
function should convert first string in lowercase and second string in uppercase.
Ans:
<html>
<body>
<script>
function convert(a,b)
{
document.write(a.toLowerCase()+"<br>")
document.write(b.toUpperCase()+"<br>")
}
var a=prompt("enter 1 string")
var b=prompt("enter 2 string")
</script>
<button type="button" onclick='convert(a,b)'>Click Me!</button>
</body>
</html>
Output:
Q 12: Declare a function ‘stringsjava’ in JavaScript to accept two strings arguments.
a. Convert both the strings to Uppercase
b. Search for string1 in string2 and display the string if found.
c. Replace all occurrences of letter ‘i’ with ‘ # ‘ in string2.
d. Display first character of string1.
Ans:
<html>
<body>
<script>
function convert(a,b)
{
document.write(a.toUpperCase()+"<br>")
document.write(a.match(b)+"<br>")
document.write(b.toLowerCase()+"<br>")
document.write(b.replaceAll('i','#')+"<br>")
document.write(b.charAt()+"<br>")
}
var a=prompt("enter 1 string")
var b=prompt("enter 2 string")
</script>
<button type="button" onclick='convert(a,b)'>Click Me!</button>
</body>
</html>
Q 13: Write a JavaScript program used to create a function len_words(s),
accept a string argument and find the sum of length of the words and display
it as follows:
Input: ‘Genius is one percent inspiration, 99% perspiration
Output:
Genius 6
is 2
one 3
percent 7
inspiration, 12
99% 3
perspiration 13
sum of length of the words= 46
Ans:
<html>
<body>
<script>
function len_words(s)
{
var sum=0
for (i=0;i<s.length;i++)
{
if(s[i]!=' ')
{
document.write(s[i]+ ‘ ‘+ s[i].length);
sum=sum+s[i].length
}
else
{
document.write("<br>")
}
document.write(“sum of length of the words”+sum)
}
var str=prompt("enter a sentence")
</script>
<button type="button" onclick='len_words(s)'>Click Me!</button>
</body>
</html>
Q 14. Write a JavaScript program used to create a function display( ) in
which consider the following array:
var cars = ["Honda", "BMW", "Audi", “Porsche”];
and the defined function is ued to do the following tasks:
a. add an item “Volvo “ to the array cars in the last.
b. remove first element from the array.
c. display number of elements in the array.
d. add following array to an array “cars”.
var person=[“Rajan”, “Yagya”, “Munish”];
Solution:
<html>
<body>
<script>
function display( )
{
var cars = ["Honda", "BMW", "Audi", "Porsche"];
var person=["Rajan", "Yagya", "Munish"];
value=prompt("Enter the name of person")
cars.push(value)
document.write(cars+"<br>")
first_ele=cars.shift()
document.write("Removed Element"+first_ele+"<br>")
document.write("After removing first element from the array"+cars+"<br>")
document.write("Display number of elements in the array"+cars.length+"<br>")
var new_array=cars.concat(person)
document.write("New Array after concatenation"+new_array)
}
</script>
<form>
<input type="button" onclick="display()" value="submit">
</form>
</body></html>
Q15. Write a JavaScript program to find the sum of squares of a all the
elements.
Ans:
<html>
<body>
<script>
function sum_sq(array)
{
var sum = 0,
i = array.length;
while (i--)
{
sum += Math.pow(array[i], 2);
}
return sum;
}
sum_sq([0, 1, 2, 3, 4])
</script>
</body>
</html>
Q17: Write a JavaScript program to create a function fun( ) and call that function by using
event onload.
Ans:
<html>
<head>
<script>
function fun() {
alert("Hello World!!, Welcome to the javaTpoint.com");
}
</script>
</head>
<body onload = "fun()">
<h1> Example of the HTML onload attribute </h1>
<p> Try to refresh the document to see the effect. </p>
</body>
</html>
Output:
Q18: Write a JavaScript program to create a function fun( ) and call that function by using
event onmouseover .
Ans:
<html>
<head>
<script>
// this function will execute when onmouseover is triggered
function func()
{
// an alert will pop-up when onmouseover gets triggered
alert("onmouseover alert.");
}
</script>
</head>
<body>
<!-- creating an onmouseover event for a <b> tag-->
<b onmouseover = "func()">
Bring the cursor here to get an alert.
</b>
</body>
</html>
Q19: Java script program used to create a function that popups a message
when a key is pressed:
Ans:
<html>
<head>
<script>
<!--
function myFunction() {
alert("hey…you pressed a key now.")
}
//-->
</script>
</head>
<body>
<input type = "text" onkeydown = " myFunction()">
</body>
</html>
Output:
Q20: Java script program used to create a function that changed the color of
the text when the focus has been shifted.
Ans:
<html> <body>
<input type="text" id="id_1" onChange="show()">
<script>
function show(){
var x=document.getElementById('id_1');
x.style.color='red';
}
</script> </body> </html>
Output: