Javascript Math Object functions

0 comments

Object : Math Methods

Math object has the following methods that can be
used to do specific & basic operations like getting random numbers, rounding
a number, square root calculation, log calculation etc.. in javascript

Here is the reference showing these functions




Function

Example Code

Result
Description

abs(value)

Math.abs(2.822);

document.write(Math.abs(2.822));
2.822
abs()
method returns the absolute value of the argument passed.

ceil(value)

Math.ceil(3.44);

document.write(Math.ceil(3.44)); 4

ceil()
method returns the nearest greater integer value of the value passed as
argument. The argument can be an integer or float.

floor(value)

Math.floor(2.44);

document.write(Math.floor(2.44)); 2

floor()
method returns the nearest lowest (least) integer value of the argument
passed.

log(value)

Math.log(3);
document.write(Math.log(3)); 1.0986122886681098

log(x)
function returns the natural logarithm of the value used.

max(value1,
value2)

Math.max(14,
12);

document.write(Math.max(14, 12)); 14

Math.max(value1,
value2) compares the two values and returns the maximum value of the two
values passed.

min(value1,
value2)

Math.min(14,
12);

document.write(Math.min(14, 12)); 12

Math.min(value1,
value2) compares the two values and returns the minimum value of the two
values passed.

pow(value1, value2)

Math.pow(4,
2);

document.write(Math.pow(4, 2)); 16

Math.pow(value1,
value2) function is used to calculate x y calculation. Here it is 4 2 .

random()

Math.random()

document.write(Math.round(Math.random()*100)); 4

Math.random()
gives a random value between 0 and 1.

round()

Math.round(9.678);

document.write(Math.round(9.678)); 10

Math.round()
function is used to round a float (decimal) value to the nearest integer
value. i.e. round of 4.5 will give 5, rounding of
4.4 will give 4.


use javascript eval() function to convert json text string into an object

0 comments
To convert a JSON text into an object, you can use the eval() function. eval() invokes the JavaScript compiler. Since JSON is a proper subset of JavaScript, the compiler will correctly parse the text and produce an object structure. The text must be wrapped in parenthesis to avoid tripping on an ambiguity in JavaScript's syntax.

for example,



var json_text = "{type:'select',rate:'10',free:'0',oprtation:'+',value:'1'}";
var json_object = eval('(' + json_text + ')');

Here is the formal definition of eval function :


The eval function is very fast. However, it can compile and execute any JavaScript program, so there can be security issues. The use of eval is indicated when the source is trusted and competent. It is much safer to use a JSON parser. In web applications over XMLHttpRequest, communication is permitted only to the same origin that provide that page, so it is trusted. But it might not be competent. If the server is not trustfully accurate in its JSON encoding, or if it does not seriously validate all of its inputs, then it could deliver invalid JSON text that could be carrying dangerous script. The eval function would execute the script, unleashing its malice.

eval()  Definition and Usage

The eval() function evaluates and/or executes a string of JavaScript code.
First, eval() determines if the argument is a valid string, then eval() parses the string looking for JavaScript code. If it finds any JavaScript code, it will be executed.

Syntax

eval(string)

Parameter Description
string Optional. The string to be evaluated/executed

An introduction to : How CGI scripting works

0 comments
The blog's name is "Web Scripting" and now I am talking about "CGI scripting"

So, what is the difference ?

I am not sure what exactly "Web Scripting" means. I know that it is used as a synonym of all programming and scripts related to web. But the exact formal definition, if anyone of you know, please let me know.

And, CGI Script,

CGI = Common Gateway Interface is an agreement between HTTP server implementors about how to integrate scripts and programs.

The task of a webserver is to respond to requests for webpages issued by clients (usually web browsers) by analyzing the content of the request (which is mostly in its URL), determining an appropriate document to send in response, and returning it to the client.
If the request identifies a file on disk, the server can just return the file's contents.
Alternatively, the document's content can be composed on the fly. One way of doing this is to let a console application compute the document's contents, and tell the web server to use that console application. CGI specifies which information is communicated between the webserver and such a console application, and how.Here is the brief description to understand "How it really works"

I love this website "howstuffworks.com" for it's collection of articles on various new and up to date topics. That's why I have added the whole article here so that you can understand Why I said "I Love It".


You can read it directly from HowStuffWorks.com


Hello World

0 comments
Hi,

I may sound a little bit monotonous or conventional with this Title here. This is the first programming I did :) I think this is true for many of you too.

That's why I have started my first blogging with "Hello World". :D

Now, I will start by explaining why I have started blogging here.

For the last 2 years, I have been familiar with web scripting. I am using this term "web scripting" to define all scripting languages that are used in website building. Although I have done most of my professional works in PHP and academic works in ASP, but it is amazing that the first scripting language I learned was Perl.

Here, in this blog I will write about all the significant knowledge about web scripting so far I had to learn and most importantly about my daily findings about this topic.

At this moment, I think topics of this blog will be about
  • PHP
  • Javascript
  • CSS
  • jQuery (this is a javascript framework)

    and others.

    And, all other web related topics that I am working / I will be working with.