parseInt in JavaScript

As per the MDN document parseInt() method-

Parses a string argument and returns an integer of the specified radix or base.

SyntaxparseInt(string, radix);

Parameter 1 (string) –

The value to parse.

Parameter 2 (radix) –

An integer that represents the radix of the above mentioned string, it is an optional parameter to be passed, however it is strongly recommended to set a one to avoid confusion.

Return type –


If the radix parameter is missing then JavaScript assumes the following:-

  • If the string begins with “0x”, the radix is 16 (hexadecimal)
  • If the string begins with “0”, the radix is 8 (octal) or 10 (decimal). This feature is browser dependent. ECMAScript 5 specifies to use 10, but not supported in many browsers so keeping your hands clean it is always a good practice to mention radix.
  • If the string begins with any other value, the radix is 10 (decimal)

Now Let us consider some examples about its implementation and check return values-

  1. document.write(parseInt()); // return NaN
  2. document.write(parseInt( )); // return NaN
  3. document.write(parseInt(01)); // return 1 as 0*81 + 1*80
  4. document.write(parseInt(010)); // return 8 as 0*82 + 1*81 + + 0*80

Explanation – As mentioned If a number starts with 0 the method will take radix as octal or 8 by default. [This is the case in my browser test as mentioned it may also take 10(decimal) depending upon ECMAScript adaption]

Moving to next level, lets us cover some string examples as well –

  1. document.write(parseInt(“a”)); // return NaN
  2. document.write(parseInt(“7a”)); // return 7 surprised!!
  3. document.write(parseInt(“017s”)); // return 15 as 0*82+1*8*1+7*80

Now I believe you are in a position to understand some dirty examples as well. Its time to check radix involvement now –

    • document.write(parseInt(14,8)); // return 12 as 1*81 + 4*80
    • document.write(parseInt(“14”,8)); // return 12, self explanatory if u followed above examples well
    • document.write(parseInt(“A”,16)); // return 10, interesting isn’t it!!! well this is because A is considered as 10 in HEXADECIMAL system, so the string will be consider as 10*160
    • document.write(parseInt(“AB”,16)); // return 171, as mentioned in above example-> A as 10, B as 11, so it will go like this, 10*161+ 11*160
    • document.write(parseInt(“ABC”,16)); // return 2748 as 10*162 + 11*161+ 12*160
    • document.write(parseInt(“S1”,16)); // return NaN, as S does not belongs to HEXADECIMAL code

NOTE– In above example a and A are at same levels so document.write(parseInt(“abc”,16)); will output the same 2748 on the same grounds.

Hope you are now in a better situation to actually answer weird behaviors in parseInt() method.

Let me know your feedback as always πŸ™‚


How to install devise in rails for user authentication

From the docs
Devise is a flexible authentication solution for Rails based on Warden. It:

  • Is Rack based;
  • Is a complete MVC solution based on Rails engines;
  • Allows you to have multiple roles (or models/scopes) signed in at the same time;
  • Is based on a modularity concept: use just what you really need.

Now let us start with some real stuff πŸ™‚

Create new rails project by running the following command –

rails new devise_install

Now open your GEM file (located at the base root of your folder, look for Gemfile in your app)

Add these two lines in your gemfile –

gem 'devise'

gem 'omniauth'

Now navigate to your app folder and run following command –

bundle install

Un comment bcrypt-ruby in gem file to use Active mode has_secure_password

Once you are done, run the following command –

rails generate devise:install

This command will generate an initializr to describe all devise configuration options
You MUST read those points before proceeding further.

Now we are ready to use it with MODEL.

Run the following command –

rails generate devise User

It will create a User model if not exists previously.

You will see some new files generated after the command. To this point I am interested in two files only one is under db/migrate folder i.e 20130321042152_devise_create_users.rb (in my case) and another one in app/model i.e user.rb

Now change 20130321042152_devise_create_users.rb to something like this –

And your user.rb file will go something like this –

Finally run this command –

rake db:migrate

Once all done go to http://localhost:3000/users/sign_in and you will see the devise magic πŸ™‚

NOTE – Do not forget to START your server and if you are facing some trouble with rail routes un comment the following line in your routes.rb file

match ':controller(/:action(/:id))(.:format)'

If you still not clear with the file structure you can PULL the code from github

Program to find prime numbers using PHP

This is a simple program in PHP to find prime numbers using for loop and ifΒ  statement.

//Program to find prime numbers in between a range

$num =23;

for( $j = 2; $j <= $num; $j++ )
for( $k = 2; $k < $j; $k++ )
if( $j % $k == 0 )

if( $k == $j )
echo “Prime Number : “, $j, “<br>”;


Prime Number : 2
Prime Number : 3
Prime Number : 5
Prime Number : 7
Prime Number : 11
Prime Number : 13
Prime Number : 17
Prime Number : 19
Prime Number : 23


Posted in PHP

How to add a particular class in last div using jQuery

Most of the time I need to add class last to my last div to have another styling for the last div.


like –


<div class=”myarticle”></div>

<div class=”myarticle”></div>

<div class=”myarticle”></div>

<div class=”myarticle”></div>

<div class=”myarticle”></div>


While this is good if all these divs being created dynamically, but to have different styling for the last div, let say like this is the require structure we like to have on our page –


<div class=”myarticle”></div>

<div class=”myarticle”></div>

<div class=”myarticle”></div>

<div class=”myarticle”></div>

<div class=”myarticle lastdiv”></div>


To add the last in our last div we use jQuery .addClass() method.


Usage – jQuery(‘div.myarticle:last-child’).addClass(‘lastdiv’);


This single line jquery adds lastdiv class to our last div.


Final output –


<div class=”myarticle”></div>

<div class=”myarticle”></div>

<div class=”myarticle”></div>

<div class=”myarticle”></div>

<div class=”myarticle lastdiv”></div>


Fibonacci Series in php

Fibonacci series is a sequence of number generated by adding the previous two terms.


1, 2, 3, 5, 8, 13, 21,….. and so on.

At a first glance you can check that, starting from 3rd term its an addition of previous two values.

You can also write a simple program in PHP for this series generation –

$a = 1;
$b = 2;
$term = 10;
$i = 0;

echo $a." ".$b." ";
for($i; $i < $term -2 ; $i++)
$c = $b +$a;
echo $c." ";
$a = $b;
$b = $c;

Output –

1 2 3 5 8 13 21 34 55 89

Posted in PHP

When use of back ticks in table name solves my problem in PHP/mysql

While playing with the code today, I found an interesting benefit of using back ticks in the table name in mysql query.

I created a table in my database as “abc.test” and added some dummy data to the table as shown in below screenshot.

table with a dot

Now with a simple mysql_query I was trying to fetch the data but its returning nothing, after a check I found that i missed those back ticks from my table name.

Incorrect Query (without back ticks)

mysql_query(“SELECT * FROM abc.test”);

Correct Query (with back ticks)

mysql_query(“SELECT * FROM `abc.test`”);

My whole working php code, to fetch data –

$conn = mysql_connect('localhost','root','');
if (!$conn)
die('Could not connect: ' . mysql_error());

mysql_select_db('stcktest', $conn);

$result = mysql_query("SELECT * FROM `abc.test`");


while ($row = mysql_fetch_assoc($result) )
echo "Id is : ".$row['id']." "." Name is : ".$row['name'];
echo "

How to create Ruby on Rails Application

  • I am developing RoR applications on Windows 7.
  • Assuming that you installed Rails framework on your system before creating application.

Type rails as shown in screenshot and click on command prompt for Rails framework.

Now being a php developer i already have XAMPP installed on my machine and i want my projects to come under the htdocs.
I created a new directory in htdocs as railrepo to store my rail projects. I am naming my first project as project1.

Check this screenshot for directory structure –

Now under railrepo directory I am creating my First rail project(project1) using new command.

rails new project1

Type this command under your railrepo folder as shown in sceenshot-

Once you are done with files creation, you are expecting this to show on your browser. For this you need to start rails server first. Type this command in the folder you created your application by opening another command prompt window.

Command – rails server

Check the screenshot –

Once your rails server started check the action in your browser, by typing —


Check the screenshot as well–

Search and replace within a column in mysql

In my codeigniter project I need to add slug field in the database from name column. This is what i did –

First of all i created a field name “slug” just after ename column and copied that content of ename to slug, using this query-

UPDATE default_event
SET `slug` = `ename`;

Then i fire this query to replace blank spaces with “-” to form links-

UPDATE default_event
SET slug = replace(slug, ‘ ‘, -‘);