Upload
sonal-maheshwari
View
259
Download
4
Embed Size (px)
Citation preview
8/3/2019 Javascript Ass Ans
1/9
1 CA 731
Date:12 Sep 2011 Submitted by: Sonal MaheshwariASSIGNMENT 1 205109081Web Technology MCA V
Q1. Create an array of five pictures. Use a for loop to display the values stored there.
Now add two more pictures to the end of the array and sort the array. (UseJavaScript's built-in array methods.) Display the sorted array. Display the picturesalso.
var arr=new Array(5);
arr[0]="a.jpg";
arr[1]="c.jpg";
arr[2]="b.jpg";arr[3]="e.jpg";
arr[4]="c.jpg";
document.write("Array of five pictures!
");
for(i=0;i
8/3/2019 Javascript Ass Ans
2/9
2 CA 731
colors[red]="RED".fontcolor("red");
colors[green]="GREEN".fontcolor("green");
colors[blue]="BLUE".fontcolor("blue");
color[white]="WHITE".fontcolor("white");document.write("Colors!");
for(i in colors){
document.write(colors[i]+"
");
}
Q3. Create a function that will return the current month by its full name. Use theDate object to get the current month. Months are returned as 0 for January, 1 forFebruary, 2 for March, etc. Output should resemble:The current month is January.Hint: You can create an array, starting at index 0, and assign a month value to it;e.g., month[0]="January" or use a switch statement, e.g., case 0: return "January".
function getmon(x)
{switch(x)
{
case 0:return("January");
case 1:return("February");
case 2:return("March");
case 3:return("April");
case 4:return("May");
case 5:return("June");
case 6:return("July");
case 7:return("August");
case 8:return("September");
case 9:return("October");
case 10:return("November");
case 11:return("December");
}
}
var thisDate=new Date();
document.write("The current month is
"+getmon(thisDate.getMonth())+".");
8/3/2019 Javascript Ass Ans
3/9
3 CA 731
Q4. An invoice is due and payable in 90 days. Write a function that will display thatdate.
function findduedate()
{
var invDate=new Date();
var dueDate=new Date();
dt=prompt("Enter the invoice date details:-Date(dd):");
mon=prompt("Enter the invoice date details:-
Month(mm):");
yr=prompt("Enter the invoice date details:-Year(yyyy):");
mon=mon-1;
invDate.setDate(dt);
invDate.setMonth(mon);
invDate.setFullYear(yr);
//90 days =(90*24*60*60*1000) = 7776000000milliseconds
dueDate.setTime(invDate.getTime()+7776000000);
document.write("Due Date for the invoice is
"+dueDate.toDateString()+"");
}
Q6. To calculate the balance on a loan, the following formula is used:PV = PMT * ( 1 - (1 + IR )-NP) / IR
PV is the present value of the loan; PMT is the regular monthly payment of theloan; IR is the loan's interest rate; NP is the number of payments remaining. Write aJavaScript statement to represent this formula.
8/3/2019 Javascript Ass Ans
4/9
4 CA 731
Enter the monthly payment amount of the loan :
Enter the interest rate of the loan :
Enter the number of payments remaining :
Q7. Using the formula to calculate the loan balance from the last exercise, write afunction that will calculate the principle balance left on a loan where the monthlypayments are $600, the annual interest rate is 5.5%, and there are 9 yearsremaining on the loan. Use the toFixed() Number method to format the output.
8/3/2019 Javascript Ass Ans
5/9
5 CA 731
document.write("Ceil : "+Math.ceil(num)+"
");
document.write("Floor : "+Math.floor(num)+"
");
document.write("Round : "+Math.round(num)+"
");
Q9. Create an array of 10 fortune cookies that will be randomly displayed each timethe user reloads the page.
var a= new Array("A stitch in time saves 9","Too many cooks spoil
the broth", "Charity begins at home", "Spare the rod, spoil the
child", "Face is the index of mind");
function fortunecookie()
{
r=Math.random();
r=Math.floor(r*5);
document.write(""+a[r]+"");
}
Q10. Create a string prototype that can be used to create an italic, Verdana font,point size 26.
function stringobj()
{
return (""+(this.toString()).italics().fontsize("26pt")
+"");
}
String.prototype.fun = stringobj;
document.write("'Hello' in Verdana, 26 point size,
italicised
");
document.write("Hello".fun());
8/3/2019 Javascript Ass Ans
6/9
6 CA 731
Q11. Calculate the circumference of a circle using the Math object.
8/3/2019 Javascript Ass Ans
7/9
7 CA 731
Q13. Use the Date object to print today's date in this format:Today is Friday, June 16, 2003.
Javascript Test
var thisDate=new Date();
document.write("Today is
"+thisDate.toLocaleDateString()+"");
Q14. Calculate and display the number of days until your next birthday.
Javascript Test
function calc()
{
var thisDate=new Date();
var bDate=new Date();
var mon=prompt("Enter the month(mm) of your birthday:");
var dt=prompt("Enter the date(dd) of your birthday: ");
mon=mon-1;
bDate.setMonth(mon);
bDate.setDate(dt);
if((mon
8/3/2019 Javascript Ass Ans
8/9
8 CA 731
Calculating number of days until your next birthday...
Q15. Create a prototype for the Date object that will print the months starting at 1instead of 0. Create a String object containing "Jose lived in San Jose for manyyears."
Javascript Test
8/3/2019 Javascript Ass Ans
9/9
9 CA 731
Q17. Get the substring ear from years.
var str="years"document.write(""+str.substring(1,4)+"");
Q18. Display the string in a blue, italic font, point size 12, all uppercase.
Javascript Test
var str="Jose lived in San Jose for many years.";
str=str.fontcolor("blue").italics().fontsize("12pt").toUpperCase();
document.write(str);