Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts
Wednesday, July 10, 2013

How To Show Different Greetings at Different Times During The Day Using Javascript

0 comments
To show different greetings for different times of day we use if else loop.here is a example which help you to sow different greetings of the day.

<HTML><HEAD><TITLE>Examples of using a Date object and the if and if else statements.</TITLE></HEAD><BODY><PRE> This script provides examples of using the Date object to obtain the hour of the day. The hour is used to print different messages at different times during the day. 
<SCRIPT Language="JavaScript"> 
now = new Date()                 //create a Date object named: now
hour = now.getHours()         //hours range from 0 to 23.
                                           //0 is midnight to before 1 AM.  
noon = 12                          //Afternoon begins at noon.
evening = 18                     //Evening begins at 6 PM.
if (hour < noon)
document.writeln("Good Morning.")
else if (hour < evening)
document.writeln("Good Afternoon.")
else
document.writeln("Good Evening.")
</SCRIPT>
</PRE>
</BODY>
</HTML>

OUTPUT :This script provides examples of using the Date object to obtain the hour of the day. The hour is used to print different messages at different times during the day.
Good Evening.
NOTE: Here the msg is shown good evening because hour>afternoon i.e. the third loop is used.
Read more...
Monday, July 8, 2013

How To Show Time and Date Using Javascript

0 comments
In this article,We can show time and date using javascript.
now = new Date() 
JavaScript provides a Date object that makes dealing with dates and times relatively easy.In this line a new Date object named now is created using the new object creation operator. This is a little different from how objects have been used in the examples up until now. String objects can be created by assigning literal text to a variable and then treating that variable as though it has properties and methods. The document and Math objects are created by the browser and interpreter environments and are ready to use by any script executed by them. The Date object however is designed to provide a way to create Date objects for a given date or time. The sample line creates a Date object named now that contains the date and time when the now Date was created. Like the String, document, and Math objects the Date object provides numerous useful methods for working with dates and times. 
hour = now.getHours() 
Just one of the useful methods provided by the Date object is the getHours() method. getHours()returns the hour of the time stored in the now variable. The hour is between 0 (midnight) and 23 (11 PM). If the time was between noon and less than 1 PM (13 hours) getHours() will return 12.

Here is a example,

<HTML>
 <HEAD>
<TITLE>How To Show Time and Date Using Javascript Simple Eample</TITLE>
 </HEAD>
 <BODY>
 <PRE>This Script help you to show time and date.
 <SCRIPT Language="JavaScript">

 now = new Date()                     //create a Date object named: now
 hour = now.getHours()            //hours range from 0 to 23.
                                               //0 is midnight to before 1 AM.
  document.write(now)
 </SCRIPT>
 </PRE>
 </BODY>
 </HTML> 

Output : This Script help you to show time and date. Mon Jul 08 2013 13:18:03

NOTE:THE TIME SHOW HERE IN THIS OUTPUT IS THE TIME WHEN I POST THIS ARTICLE.
Read more...
Tuesday, May 7, 2013

How to calculate roots of Quadratic Equation using Javascript

0 comments
Quadratic Equations are of the form ax2 + bx + c = 0. To find roots(root1 and root2) of such an equation, we need to use the formula:
how to find Roots of Quadratic Equation: JavaScript
Roots of Quadratic Equation

Javascript code for calculating roots of quadratic equation is given below:

 <html>
<title>Root calculator</title>
<head>
    <script type="text/javascript">
    function roots()
    {
        var a = prompt("Enter value of a","");
        var b = prompt("Enter value of b","");
        var c = prompt("Enter value of c","");

        var root_part = Math.sqrt(b * b - 4 * a * c);
        var denom = 2 * a;

        var root1 = ( -b + root_part ) / denom;
        var root2 = ( -b - root_part ) / denom;

        document.write("1st root: "+root1+"<br />");
        document.write("2nd root: "+root2+"<br />");
    }
    </script>
    </head>
<body>
    <input type="button" onclick="roots()" value ="Root calculator" >
    </body>
</html>
 Demo for roots of Quadratic Equation using Javascript:
  • click the Root calculator button
  • simply enter the values of a, b & c
  • calculated roots will be shown
Read more...

Labels

 
How To 99 © 2013 How to 99 &

How to Tutorial in 99 Categories