Showing posts with label JK. Show all posts
Showing posts with label JK. Show all posts

Tuesday, September 7, 2021

Paypal Smart Button Integration using Server Side Order API V2




Please find the source and instructions @ https://github.com/kartheekgj/paypalnodeIntegration


Async Await Simple example


let url1 = "https://reqres.in/api/users";
async function users() {
    let response = await fetch(url1);
    let user = await response.json();
    return user;

};

let singleUrl = "https://reqres.in/api/users/";
async function userData(data) {
    let url = singleUrl + data;
    let response = await fetch(url);
    let user = await response.json();
    return user;
};


users().then(respData => {
    console.log(respData.data);
    var userid = respData.data[0]['id']
    userData(userid).then((responseData=> {
        console.log(responseData);
    });
});

Thursday, August 28, 2014

Example for Closure, .call, .apply in javascript


function add(y,z){ 
function fn(x,y,z){ 
console.log(Math.round(x+y)*z);
};
fn(this.x,y,z);
};

add.call({x: 20.1},3.1,5.1);  // 117.3
add.apply({x: 20.1},[3.1,5.1]);  // 117.3

Wednesday, July 30, 2014

Strangest things i found in JavaScript


  1. [] + []; // ""
  2. {} + {}; // NaN
  3. [] + {}; // "[object Object]"
  4. {} + []; // 0
  5. [] == []; // false
  6. [] < []; // false
  7. [] > []; // false
  8. [] <= []; // true
  9. [] >= []; //true
  10. Math.min() < Math.max(); // false
  11. foo = [0];
  12. !foo; // false
  13. foo == foo; // true
  14. foo == !foo; // true
  15. [['0']] == false; // true
  16. [['0']] == true; // false
  17. typeof NaN; // "number"
  18. NaN == true; // false
  19. NaN == false; // false
  20. NaN != true; // true
  21. NaN != false; // true
  22. NaN == NaN; // false
  23. NaN == !NaN; // false
  24. !NaN; // true
  25. NaN != NaN; // true
  26. 'x' == true; // true
  27. 'x' == false; // false
  28. '0' == true; // false
  29. '0' == false; // true

Friday, July 25, 2014

Couple of interview questions

Write a function that takes an integer (i) and prints out the first 'i' rows of Pascal's Triangle (eg. i =5).

            line = 5;
            var pascalDepth = parseInt(line, 10); //The input integer is provided on a single line as a text value, so this handles it.
            var testTriangle = pascalTriangle(pascalDepth);
            var output = [];
            maxlength = testTriangle.length;
            var html = "<table>"
            for(var i = 0; i < maxlength; i++){
                html += "<tr>";
                html += "<td>";
                html += testTriangle[i];
                html += "</td>";
                html += "</tr>";
            }
            html+= "</table>";

            document.write(html);

            function pascalTriangle(totalLines, triangle) {
                if (typeof triangle === 'undefined') {
                    triangle = [];
                }

                if (triangle.length === totalLines) {
                    return triangle;
                }

                if (triangle.length > 0) {
                    var triangleLine = [];
                    for (var i = 0; i < triangle.length; i++) {
                        if (typeof triangle[triangle.length - 1][i - 1] === 'undefined') {
                            triangleLine.push(1);
                        } else {
                            triangleLine.push((triangle[triangle.length - 1][i - 1] + triangle[triangle.length - 1][i]));
                        }
                    }
                    triangleLine.push(1);
                    triangle.push(triangleLine);
                } else {
                    triangle.push([1]);
                }
                return pascalTriangle(totalLines, triangle);
            }
-----------------------------------------------------------------------------------------

Write a function that prints out a breakdown of an integer into a sum of numbers that have just one non-zero digit.  

eg., given 43018 it should print 40000 + 3000 + 10 + 8.

            var arr = [];
            var i =1;               
            var a = 40118;
            var input = 40118;
            while(a > 0)
            {
            if(a%(Math.pow(10,i)) > 0)
              arr.push(a%(Math.pow(10,i)));       
             
              a = a-a%(Math.pow(10,i++));
            }
            arr.sort(function(a,b){ return b-a;});
            document.write(input +" = " +arr.join(" + "));

Thursday, July 17, 2014

Creating a URL shortner

Here is the actual code i have created using php its very simple and easy to use.

Here is the live Working url : http://shorturl-kartheekgj.rhcloud.com/


Please find the code at:   https://github.com/kartheekgj/shorturl



Wednesday, May 7, 2014

Swap 2 numbers without using third variable

This is a very very very simple question generally asked as first level of logical questions

Swap 2 numbers without using third variable


function fnSwap(a,b){
   console.log("Initial value of a: "+ a);
   console.log("Initial value of b: "+ b);

   // swaping the values of a and b
    b = a+b
    a = b-a
    b = b-a
    console.log("Swaped value of a: "+ a);
   console.log("Swaped value of b: "+ b);
}

Input:

fnSwap(10,20);


output:

Initial value of a: 10
Initial value of b: 20
Swaped value of a: 20
Swaped value of b: 10

Note:
It works with console if you are working with firefox install firebug to see output in console
if you install chrome/IE you can use f12 for console

Friday, February 14, 2014

Change your Facebook Username

This is a little hacky type of method but it works

Step 1 
  • Go to account settings from your
  • Click on ‘Security’
  • Then click the option ‘Deactivate your account.’
  • Give explanation as “Its temporary”.

Step 2
  • Then log in again with your email and password.
  • Done
  • Your account is re-activated.
Step 3
  • Go to account settings from the button on top right.
  • Edit username.
  • Done. Your account username is changed.

Friday, June 28, 2013

Create QR code generator using php-curl


    $url = "https://chart.googleapis.com/chart?";
    $strPost = "cht=qr&chs=177x177&chl=http://opendummies.blogspot.in/";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    // curl_setopt($ch, CURLOPT_PROXY, $proxy);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_HTTPGET,1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: image/jpeg"));
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $strPost); // add POST fields
    curl_setopt($ch, CURLOPT_POST, 1);
    header('Content-Type: image/jpeg');
    echo $result = curl_exec($ch);
    curl_close($ch);
    

output:

Tuesday, June 25, 2013

URL shortner using Bitly - PHP

For creating a shorten url you need to create an account in the following link:

http://dev.bitly.com/my_apps.html

Now go to 

https://bitly.com/a/oauth_apps 

Here you need to create a accesstoken if you are creating for the first time it prompts for the password.

Then you get a alpha numeric value as accesstoken section.


Now use the following Code:

    $url = "https://api-ssl.bitly.com/v3/shorten";
    $strPost = "longUrl=http%3A%2F%2Fgoogle.com%2F&   access_token=YOUR ACCESSTOKEN";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    // curl_setopt($ch, CURLOPT_PROXY, $proxy);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_HTTPGET,1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/json"));
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $strPost); // add POST fields
    curl_setopt($ch, CURLOPT_POST, 1);
    $result = curl_exec($ch);
    var_dump($result);
    curl_close($ch);


The output of this file will return a json string
 
{
    "status_code" : 200,
    "status_txt" : "OK",
    "data" : {
        "long_url" : "http:\/\/google.com\/",
        "url" : "http:\/\/bit.ly\/14UO8lv",
        "hash" : "14UO8lv",
        "global_hash" : "900913",
        "new_hash" : 1
    }
}



NOTE
The bitly code is to be used for personal accounts cannot be used for Enterprise use if you want to use it for Enterprise you need to request them for the details but this code even works for that Code too.

Google shorten URL - a Simple CURL script


    $url = "https://www.googleapis.com/urlshortener/v1/url";
    $strPost = '{"longUrl": "YOUR LONG URL"}';
 
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    //if your are using proxy url use proxy setting
    // curl_setopt($ch, CURLOPT_PROXY, $proxy);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_HTTPGET,1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/json")); //since i am requesting json response
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $strPost); // add POST fields
    curl_setopt($ch, CURLOPT_POST, 1);
    $result = curl_exec($ch);
    var_dump($result);
    curl_close($ch);
    exit;

If you dont want to use the google url shortner want to create your own


check it here :
http://opendummies.blogspot.in/2014/07/creating-url-shortner.html

Thursday, December 6, 2012

Convert First letter a string to capital

Here is the function which is used to convert all the first letters of the name to capital Letters. 

 This idea of creating this  function is inspired with the words of my colleague   Kamal Relwani

Sunday, November 4, 2012

How to create a facebook add friend request

Using JavaScript :
 Things Required all u need to do i call
  1. Call FB.init Method -
    FB.init({  appId  : 'YOUR_APP_ID',  frictionlessRequests : true });
  2. To send request for purticular friends
    1. function sendRequestToRecipients(){ FB.ui ({method: 'apprequests', message: 'My Request', to: '12345,23456' }, requestCallback); }
  1.  To send request for all friends
    1. function sendRequestViaMultiFriendSelector() {
        FB.ui({method: 'apprequests',
          message: 'My  Request'
        }, callbackfunction());
      }
       callbackfunction(response){
       alert(response);
      }
  

Wednesday, October 3, 2012

Create a Date Drop down

Creating a Date year and Date drop down which is validated based on leap year and date 

Here is the code please add jquery script tag before you include the code


 function fnDate(){
try{
for(var i=1950;i<=2009;i++){
jq("#dobyy").append("<option value="+i+">"+i+"</option>");
}
for(var i=1;i<=12;i++){
jq("#dobmm").append("<option value="+i+">"+i+"</option>");
}

var month = '';
var year = '';
jq("#dobmm").change(function(){
month = jq("#dobmm").val();
year = jq("#dobyy").val();
var aOddMnth = ["1","3","5","7","8","10","12"];
var aEvnMnth = ["4","6","9","11"];
jq("#dobdd").html("<select name=\"dobdd\" id=\"dobdd\" title=\"1\" style=\"width:84px;\"><option value=\"\">Day</option></select>&nbsp;&nbsp;&nbsp;&nbsp;");
if(month > 0){
if(month == "2"){
if(year%4 == 0){
for(var i=1;i<=29;i++){
jq("#dobdd").append("<option value="+i+">"+i+"</option>");
}
}else{
for(var i=1;i<=28;i++){
jq("#dobdd").append("<option value="+i+">"+i+"</option>");
}
}
}
if(jq.inArray(month,aOddMnth) != -1){
for(var i=1;i<=31;i++){
jq("#dobdd").append("<option value="+i+">"+i+"</option>");
}
}
if(jq.inArray(month,aEvnMnth) != -1){
for(var i=1;i<=30;i++){
jq("#dobdd").append("<option value="+i+">"+i+"</option>");
}
}
}
});
}catch(e){
alert(e.message)
}
}

Sunday, April 1, 2012

A simple Shopping Cart code in PHP

Here is a simple way to code to code the shopping cart i have seen in many websites for the code but i hope i have done using basic simple concepts of PHP to code the Shopping cart

Here is the link to download the source code - download

Instructions for using that code
  1. Extract the ca.rar
  2. place ca folder in www folder
  3. Execute the backup file or use scripts present in dbscripts folder to create new tables
  4. Now run localhost/ca to execute the code 

 

Tuesday, March 27, 2012

Get Url From Browser in Php

Getting the URL  from the parent window when you use a popup. This small Example code might help you to get the url from the parent window this can be used normal windows also

 if(isset($_SERVER['HTTP_REFERER']))
{
   
    $strPrevScheme = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_SCHEME);
 
    $strPrevHost = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
 
    $strPrevPath = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_PATH);
 
    $strPrevURL = urlencode($strPrevScheme.'://'.$strPrevHost.$strPrevPath);
   
   
}

the parse_url(url) gives the following array 

Array
(
    [scheme] => http
    [host] => hostname
    [user] => username
    [pass] => password
    [path] => /path
    [query] => arg=value
    [fragment] => anchor
)
 

 
reference - http://in2.php.net/manual/en/function.parse-url.php 

Monday, March 26, 2012

Finding the Count of all Charecters entered


<?php
/******************************************************************************
 * Filename: totalnumberofalphabets.php
 * Description: The file is used to count the number of occurences of each charecter
 * Author: G Jayendra Kartheek
 * Date Created: 26 Mar 2012
 * Copyright © 2012 All rights reserved
 ******************************************************************************/

//after user submits the values
if (isset($_POST['butSubmit']) && '' != $_POST['butSubmit']) {

    //taking the charecter into the array
    $strValue = str_split($_POST['txtInput']);
    //copying the string array
    $strDuplicate = $strValue;
   
    $iIntI = 0;
    //based on the string length travesing the array
    while ($iIntI < strlen($_POST['txtInput'])) {
        //initializing the count and j for every iteration
        $iCount = 0;
        $iIntIIntJ = 0;
        //comparing each and every element  and finding the count of each element
        while ($iIntIIntJ < strlen($_POST['txtInput'])) {
            if ($strDuplicate[$iIntI] == $strValue[$iIntIIntJ]) {

                $iCount++;
            }
            $iIntIIntJ++;
        }
        //printing the count
        echo '<br>Number of times "' . $strDuplicate[$iIntI] . '" repeated is : ' . $iCount;
        $iIntI++;
    }
}
?>
<form name="" method ="POST">
    <input type='text' name = 'txtInput'/><input type = 'submit' name= 'butSubmit' value ="OK"/>
</FORM>

Thursday, March 22, 2012

Closing the PopUp and redirecting Parent Window

I have tried for few hours to solve this problem Here is the simple example which helps you to solve the Problem

Problem:
 Create a PopUp window on submission of a value in PopUp the PopUp Closes and the Parent Windows redirects to Some other Page 

Solution:
Here is the simple way to solve it

Create 2 php pages

1. popup.php
Copy this Code

<html>
    <head>    
    </head>
    <body>
        <a href="#" onclick="window.open('popupcode.php','Login','height=500,width=420,scrollbars=yes');return false;">PopUp</a>
    </body>
    </html>




Tuesday, March 20, 2012

Count Number of tables in a Schema

This is a very Simple query to find the number of tables present in a purticular schema of the database


select information_schema as your default schema and use the following query to find the count of number of tables in a particular schema


SELECT count(*) FROM 'TABLES' WHERE TABLE_SCHEMA = 'YOUR_SCHEMA_NAME*'



*YOUR_SCHEMA_NAME = NAME OF THE SCHEMA TO WHICH YOU ARE FINDING THE COUNT







Switch Off Foreign Key Constraint

I used to use this code regularly but when i found it for the first time it took me to read many pages to read after getting the code i thought this would be useful for all people like me who want to Switch off the Foreign key Constraint in their tables

This is helpful when you are actually want to create tables irrespective of the Foreign key constraint 

To Turn Off - SET FOREIGN_KEY_CHECKS = 0;

To Turn On - SET FOREIGN_KEY_CHECKS = 1;