0% found this document useful (0 votes)
11 views10 pages

Js Practical (

The document contains multiple JavaScript examples demonstrating various programming concepts such as finding the absolute value of a number, checking data types, determining if a number is positive, negative, or zero, and using switch statements. It also includes scripts for calculating the sum of natural numbers, summing digits of a number, sorting names in descending order, checking for palindromes, displaying the current date and checking for leap years, and using the eval function. Each example is presented in a separate HTML structure with corresponding JavaScript code.

Uploaded by

Riya Sagewar
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)
11 views10 pages

Js Practical (

The document contains multiple JavaScript examples demonstrating various programming concepts such as finding the absolute value of a number, checking data types, determining if a number is positive, negative, or zero, and using switch statements. It also includes scripts for calculating the sum of natural numbers, summing digits of a number, sorting names in descending order, checking for palindromes, displaying the current date and checking for leap years, and using the eval function. Each example is presented in a separate HTML structure with corresponding JavaScript code.

Uploaded by

Riya Sagewar
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/ 10

write a javascript to find absolute number

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
<script type="text/javascript">
var n
n=prompt("enter a number",0);
if(n<0)
n=n*-1;
document.write("absolute number is :"+n);
</script>
</body>
</html>

Output:

<!-- Pr_2 : demonstration of typeof() operand-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>typeof() operand</title>
</head>
<body>
<script type="text/javascript">
a=10;
b="xyz";
if(typeof(a)=="number")
document.writeln("data type is number"+"<br>");
if(typeof(b)=="string")
document.writeln("data type is string");
</script>
</body>
</html>

Output:-
<!-- Pr_3 : To check whether entered number is positive or negative or zero-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>positive or negative or zero</title>
</head>
<body>
<script type="text/javascript">
var n;
n = prompt("enter a number",0);
if(n>0)
document.write("number "+n+" is positive");
else
if(n<0)
document.write("number "+n+" is negative");
else
document.write("number "+n+" is zero");
</script>
</body>
</html>

Output:-

<!-- Pr_4 : consider the following example where we are offering a kind of chocolate depending
upon the age of person's age -->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>chocolate depending upon the age of person's age</title>
</head>
<body>
<script type="text/javascript">
var age;
age = prompt("enter age",0);
switch(parseInt(age))
{
case 5:
document.write("milky bar");
break;
case 18:
document.write("coffy bar");
break;
case 21:
document.write("nestly bar");
break;
default:
document.write("sorry");
}
</script> </body> </html>

Output:-

<!-- Pr_5 : Write a script to find sum of first N natural numbers.-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>sum of first N natural numbers</title>
</head>
<body>
<script type="text/javascript">
var i,n,sum=0;
n = prompt("enter a number");
for(i=1;i<=n;++i)
{
sum=sum+i;
}
document.write("sum ="+sum);
</script>
</body>
</html>

Output:

<!-- Pr_6 :Write a script to find the sum of each digit of a number.-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>sum of each digit</title>
</head>
<body>
<script type="text/javascript">
var num,rem,sum=0;
num = prompt("enter number");
while(num>0)
{
rem = num % 10;
sum = sum + rem;
num = parseInt(num/10);
}
document.write("sum of digit =" + sum);
</script>
</body>
</html>

Output:

<!-- Pr_7 :demonstration of array object for accepting and displaying list of names in
descending order .-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>array object</title>
</head>
<body>
<script type="text/javascript">
var n;
n= prompt("Enter how many elements");
a= new Array[n];
for(i=0;i<n;++i)
{
a[i] = prompt("Enter string");
}
document.writeln("Number of element in array are ="+ a.length +"<br>");
a.sort();
a.reverse();
document.writeln("Sorted list in descending order ="+"<br>");
for(i in a)
{
document.writeln(a[i]+"<br>");
}
</script>
</body>
</html>

Output:

<!-- Pr_8 : write a script to check entered string is palindrome or not .-->

<!DOCTYPE html>
<html>
<head>
<title>palindrome or not</title> </head>
<body>
<script type="text/javascript">
var s,lc,rc;
var b,e,len,half;
s=prompt("enter string");
len = s.length;
half=parseInt(len/2);
for (b=0,e=len-1;b<half && e>=half;++b, --e )
{
lc=s.charAt(b);
rc=s.charAt(e);
if (lc!=rc)
break;
}
if(b==e||(b-e==1))
document.write("string is palindrome");
else
document.write("string is not palindrome");
</script> </body> </html>

Output:

<!-- Pr_9 :demonstration of date object for displaying current day and check current year is
leap or not .-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>date object</title>
</head>
<body>
<script type="text/javascript">
var y;
d=new Date();
document.writeln("current day of month"+d.getDate()+"<br>");
document.writeln("current elapsed hour in a day"+d.getHours()+"<br>");
document.writeln("current year"+d.getYear()+"<br>");
y=d.getYear();
if(y%4==0)
document.writeln("current year is a leap year");
else
document.writeln("current year is not a leap year");
</script>
</body>
</html>

Output:

<!-- Pr_10 : demonstration of eval function .-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>eval function</title>
</head>
<body>
<script type="text/javascript">
var c;
c = eval("4+5*8/2");
document.write(c);
</script>
</body>
</html>
Output:

You might also like