0% found this document useful (0 votes)
263 views9 pages

Javascript Ass Ans

The document contains 18 questions related to JavaScript. Each question demonstrates a different JavaScript concept such as arrays, loops, functions, dates, strings, and prototypes. For example, question 1 creates an array of pictures and uses a for loop to display them. Question 2 uses an associative array and for/in loop to display colors in different fonts.

Uploaded by

Sonal Maheshwari
Copyright
© Attribution Non-Commercial (BY-NC)
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)
263 views9 pages

Javascript Ass Ans

The document contains 18 questions related to JavaScript. Each question demonstrates a different JavaScript concept such as arrays, loops, functions, dates, strings, and prototypes. For example, question 1 creates an array of pictures and uses a for loop to display them. Question 2 uses an associative array and for/in loop to display colors in different fonts.

Uploaded by

Sonal Maheshwari
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 9

Date: 12 Sep 2011 ASSIGNMENT 1 Web Technology

Submitted by: Sonal Maheshwari 205109081 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. (Use JavaScript's built-in array methods.) Display the sorted array. Display the pictures also. <html> <head> <script language=javascript> 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("<h3>Array of five pictures!</h3><br>"); for(i=0;i<arr.length;i++) { document.write("Picture "+(i+1)+" : "+arr[i]+"<br>"); } arr[5]="f.bmp"; arr[6]="g.bmp"; arr.sort(); document.write("<h3>New Array of seven pictures after sorting</h3><br>"); for(i=0;i<arr.length;i++) document.write("Picture "+(i+1)+" : "+arr[i]+"<br>"); document.write("<h3>Pictures:-</h3><br>"); for(i=0;i<arr.length;i++) { document.write("<img src="+arr[i]+">"); } </script> </head> </html> Q2. Create an associative array called colors. Each element of the array is a string representing the color, e.g., red or blue. Use the for/in loop to view each element of the array with a color of the font the same color as the value of the array element being displayed. <html> <head> <script language=javascript> colors=new Array();

1 CA 731

colors[red]="RED".fontcolor("red"); colors[green]="GREEN".fontcolor("green"); colors[blue]="BLUE".fontcolor("blue"); color[white]="WHITE".fontcolor("white"); document.write("<h3>Colors!</h3>"); for(i in colors) { document.write(colors[i]+"<br>"); } </script> </head> <body bgcolor=yellow> </body> </html> Q3. Create a function that will return the current month by its full name. Use the Date object to get the current month. Months are returned as 0 for January, 1 for February, 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". <html> <head> <script language=javascript> 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("<h3>The current month is "+getmon(thisDate.getMonth())+".</h3>"); </script> </head> </html>

2 CA 731

Q4. An invoice is due and payable in 90 days. Write a function that will display that date. <html> <head> <script language=javascript> 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("<h3>Due Date for the invoice is "+dueDate.toDateString()+"</h3>"); } </script> </head> <body> <input type=button value="Click to find due date!" onclick="findduedate()"/> </body> </html> 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 the loan; IR is the loan's interest rate; NP is the number of payments remaining. Write a JavaScript statement to represent this formula. <html> <head> <script language=javascript> <!-function calc() { var PMT=document.form1.pmt.value; var IR=(document.form1.ir.value)/100; var NP=document.form1.np.value; var PV = PMT*(1-(1+IR)-NP)/IR; alert("Present value = "+PV); }

3 CA 731

</script> </head> <body> <form name="form1"> <pre><h4> Enter the monthly payment amount of the loan : <input type="text" name="pmt"/><br> Enter the interest rate of the loan : <input type="text" name="ir"/><br> Enter the number of payments remaining : <input type="text" name="np"/><br> <center> <input type=button value="Find loan balance" onclick="calc()"/> </center> </h4></pre> </form> </body> </html> Q7. Using the formula to calculate the loan balance from the last exercise, write a function that will calculate the principle balance left on a loan where the monthly payments are $600, the annual interest rate is 5.5%, and there are 9 years remaining on the loan. Use the toFixed() Number method to format the output. <html> <head> <script language=javascript> <!-var PMT=600; var IR=5.5/100; var NP=9*12; var PV = PMT*(1-(1+IR)-NP)/IR; document.write("<h3>Monthly payment = 600<br>"); document.write("Interest rate = 5.5%<br>Number of payments remaining = 98 (9 years)<br>"); document.write("<br>Present value = "+PV.toFixed(2)+" (rounded to two digits)</h3>"); </script> </head> </html> Q8. Apply the ceil(), floor(), and round() methods to the number 125.5567 and display the results. <html> <head> <script language=javascript> var num=125.5567; document.write("<h3>The number : "+num+"<br>");

4 CA 731

document.write("Ceil : "+Math.ceil(num)+"<br>"); document.write("Floor : "+Math.floor(num)+"<br>"); document.write("Round : "+Math.round(num)+"<br></h3>"); </script> </head> </html> Q9. Create an array of 10 fortune cookies that will be randomly displayed each time the user reloads the page. <html> <head> <script language="JavaScript"> 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("<h3>"+a[r]+"</h3>"); } </script> </head> <body onload="fortunecookie()"> </body> </html> Q10. Create a string prototype that can be used to create an italic, Verdana font, point size 26. <html> <head> <script language=javascript> function stringobj() { return ("<font face='Verdana'>"+(this.toString()).italics().fontsize("26pt") +"</font>"); } String.prototype.fun = stringobj; document.write("<h3>'Hello' in Verdana, 26 point size, italicised<br></h3>"); document.write("Hello".fun()); </script> </head> </html>

5 CA 731

Q11. Calculate the circumference of a circle using the Math object. <html> <head> <script language=javascript> <!-function calc() { r=parseFloat(document.form1.radius.value); var c=Math.PI*2*r; alert("Circunference of the circle : "+c.toFixed(2)); } </script> </head> <body> <form name=form1> <pre> Enter the radius of the circle : <input type=text name="radius"/> <input type=button name=find value="Find circumference" onclick="calc()"/> </pre> </form> </body> </html> Q12. Write a JavaScript program that uses the Array and Math objects. Create an array of 5 sayings, for example: "A stitch in time saves 9", or "Too many cooks spoil the broth". Each time the Web page is visited, print a random saying. <html> <head> <script language=javascript> <!-var arr=new Array(5); arr[0]="A stitch in time saves 9"; arr[1]="Too many cooks spoil the broth"; arr[2]="Charity begins at home"; arr[3]="Spare the rod, spiol the child"; arr[4]="Face is the index of mind"; var ch = Math.round(Math.random()*5); document.write("<h3>"+arr[ch]+"</h3>"); //--> </script> </head> </html>

6 CA 731

Q13. Use the Date object to print today's date in this format: Today is Friday, June 16, 2003. <html> <head> <h3>Javascript Test</h3><br> </head> <body background=Sunset.jpg> <script language=javascript> var thisDate=new Date(); document.write("<h3>Today is "+thisDate.toLocaleDateString()+"</h3>"); </script> </body> </html> Q14. Calculate and display the number of days until your next birthday. <html> <head> <h3>Javascript Test</h3> <script language=javascript> 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<thisDate.getMonth())|| ((mon==thisDate.getMonth())&&(dt<thisDate.getDate()))) { bDate.setYear(thisDate.getFullYear()+1); } document.write("<font size=+2 color=blue>"); document.write("<br><br>Your birthday is next on "+bDate.toString()+"<br><br>"); document.write("Number of days until your next birthday : "+ ((bDate.getTime()-thisDate.getTime())/86400000)); document.write("</font>"); } </script> </head> <body> <font size=+2 color=blue>

7 CA 731

Calculating number of days until your next birthday...<br><br> <input type=button value="Click!" name=btn onclick="calc()"/> </font> </body> </html> Q15. Create a prototype for the Date object that will print the months starting at 1 instead of 0. Create a String object containing "Jose lived in San Jose for many years." <html> <head> <h3>Javascript Test</h3> <script language=javascript> <!-function printmon() { return(this.getMonth()+1); } Date.prototype.getMon=printmon; var d=new Date(); document.write("<h4>New Date prototype : "+d.getMon()+"<br>"); var str="Jose lived in San Jose for many years."; document.write(str+"</h4>"); </script> </head> </html> Q16. Find the index for the second Jose. <html> <head> <script language=javascript> var str="Jose lived in San Jose for many years." document.write("<h3>Second index of 'Jose' : "+((str.substr(str.indexOf("Jose")+1)).indexOf("Jose")+1)+"</h3>" ); </script> </head> </html>

8 CA 731

Q17. Get the substring ear from years. <html> <head> <script language=javascript> var str="years" document.write("<h3>"+str.substring(1,4)+"</h3>"); </script> </head> </html> Q18. Display the string in a blue, italic font, point size 12, all uppercase. <html> <head> <h3>Javascript Test</h3> <script language=javascript> var str="Jose lived in San Jose for many years."; str=str.fontcolor("blue").italics().fontsize("12pt").toUpperCase(); document.write(str); </script> </head> </html>

9 CA 731

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy