0% found this document useful (0 votes)
15 views

GUI

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

GUI

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

5-d Write a javascript program to accept a number from the user and display the sum of the digits.

<!DOCTYPE html>
<html>
<head>
<title>Sum of Digits</title>
<script language="JavaScript" type="text/JavaScript">
var n=parseInt(prompt("Enter the number", ""));
var p=0,y;
while(n>0)
{
y=n%10;
n=parseInt(n/10);
p=p+y;
}
document.write("Sum of the Digits is: " +p);
</script>
</head>
<body>
</body>
</html>

OUTPUT:

6-a Using JavaScript, design a web page demonstrating different native objects of JavaScript.

1] Array Object Code:

Output:-

2] Boolean Object Code:

<!DOCTYPE html>
<html>
<head>
<title>Boolean object demo</title>
<script language="JavaScript" type="text/JavaScript">
function myFunction()
{
document.getElementById("demo").innerHTML=Boolean(10>9);
}
</script>
</head>
<body>
<p>Display the value of Boolean(10>9):</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
</body>
</html>

Output:-

3] Date Object Code:

<!DOCTYPE html>
<html>
<head>
<title>Date object demo</title>
<script language="JavaScript" type="text/JavaScript">
var currentDate=new Date();
document.write("Date is:");
document.write(currentDate.getMonth()+"/"+currentDate.getDate()+"/"+currentDate.getFullYear()+"<br>");
document.write("Date is:");
document.write(currentDate.getHours()+":"+currentDate.getMinutes()+":"+currentDate.getSeconds());
</script>
</head>
<body>
</body>
</html>

Output:-

4] String Object Code:

<!DOCTYPE html>
<html>
<head>
<title>String object demo</title>
<script language="JavaScript" type="text/JavaScript">
var txt="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
document.write("Length: "+txt.length+"<br>");
var x="It's alright";
var y="Thank you.";
document.write(x+"<br>"+y);
var str="Please locate where 'locate' occurs!";
var pos=str.indexOf("locate");
document.write("<br>Index of Locate: "+pos);
var str="Please locate where 'locate' occurs!";
var pos=str.lastIndexOf("locate");
document.write("<br>Last Index of Locate: "+pos);
var str="Please locate where 'locate' occurs!";
var pos=str.search("locate");
document.write("<br>Search Ans: "+pos);
var str="Apple, Banana, Kiwi";
var res=str.slice(7,13);
document.write("<br>Slice: "+res);
var str="Apple, Banana, Kiwi";
var res=str.substring(7,13);
document.write("<br>Substring: "+res);
var str="Apple, Banana, Kiwi";
var res=str.substr(7,6);
document.write("<br>Substr: "+res);
var str="Pleace visit Microsoft!";
document.write("<br>"+str);
var n=str.replace("Microsoft","W3Schools");
document.write("<br>Replace: "+n);
var text1="Hello World!";
document.write("<br>"+text1);
var text2=text1.toUpperCase();
document.write("<br>Upper case: "+text2);
var text1="Hello World!";
document.write("<br>"+text1);
var text2=text1.toLowerCase();
document.write("<br>Lower case: "+text2);
var text1="Hello";
document.write("<br>"+text1);
var text2="World";
document.write("<br>"+text2);
text3=text1.concat(" ",text2);
document.write("<br>Concat string: "+text3);
var str="Hello World!";
document.write("<br>0th Place character: "+str.charAt(0));
document.write("<br>0th Place character code: "+str.charCodeAt(0));
var txt="a,b,c,d,e";
document.write("<br>"+txt);
var arr=txt.split("");
document.write("<br>"+arr[0]+" "+arr[2]+" "+arr[4]+" "+arr[6]+" "+arr[8]);
</script>
</head>
<body>
</body>
</html>

Output:-
5] RegEx Object Code:

<!DOCTYPE html>
<html>
<head>
<title>RegEx object demo</title>
<script language="JavaScript" type="text/JavaScript">
var str="Visit W3Schools!";
document.write("<br>"+str);
var pos=str.search(/w3schools/i);
document.write("<br>Search Ans: "+pos);
var str="Visit Microsoft!";
document.write("<br>"+str);
var pos=str.replace(/microsoft/i,"W3Schools");
document.write("<br>Replace Ans: "+pos);
text="The best things in life are free!";
document.write("<br>test() method: "+/life/.test(text));
text="The best things in life are free!";
document.write("<br>exec() method: "+/life/.exec(text));
</script>
</head>
<body>
</body>
</html>

Output:-
6-b Write a program in javascript to accept a sentence from the user and display the number of words in it. (Do
not use split() function)
<!DOCTYPE html>
<html>
<head>
<title>Number of words in the sentence without using split function</title>
<script language="JavaScript" type="text/JavaScript">
var str=prompt("Enter the sentence:"," ");
var count=0;
var i;
for(i=0;i<str.length;i++)
{
if(str.charAt(i,1)==" " &&str.charAt(i+1,1)!=" ")
count++;
}
document.write("Number of words are:"+(count+1));
</script>
</head>
<body>
</body>
</html>

OUTPUT:

You might also like