Monday, August 31, 2015

question no 1

<?php
//function for letter count return an assoc array
function letter_count($string){
    $length = strlen($string);
    $words = array();
    for($i = 0; $i < $length; $i++){
        if($string[$i] == " "){
            continue;
        }
        if(array_key_exists($string[$i], $words)){
            $words[$string[$i]] ++;
        }else{
             $words[$string[$i]] = 1;
        }
    }
    return $words;
}

if(isset($_POST['string'])){
   $string = $_POST['string'];
    $words = letter_count($string);
}


?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>letter count</title>
    <link rel="stylesheet" href="css/bootstrap.css">
    <style>
        .green{
            color: green;
            font-weight: bold;
        }
    </style>
</head>
<body>
<div class="container">
    <form method="post">
        <div class="form-group">
            <label for="string">give a sentence</label>
            <input type="text" name="string" class="form-control" id="string" placeholder="Enter string">
        </div>
        <button type="submit" name="" class="btn btn-default">Submit</button>
    </form>
    <?php if(isset($words)) : ?>
        <table class="table table-striped">
            <thead>
            <tr>
                <th>words</th>
                <th>frequency</th>
            </tr>
            </thead>
            <tbody>
                <?php
                    foreach($words as $key => $value) :

                ?>
                <tr>
                    <td><?php echo $key; ?></td>
                    <td class="<?php if($value > 5){echo 'green';} ?>"><?php echo $value; ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        <?php endif ;?>
</div>

</body>
</html>

Sunday, August 30, 2015

Basic validation in php

<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){
    $errors = array();
    if(empty($_POST['name'])){
        $errors['name'] = "You have to enter name to in name field";
    }else{
        $name = $_POST['name'];
    }
    if(empty($_POST['email'])){
        $errors['email'] = "You have to enter email adddress to in email field";
    }else{
        $email = $_POST['email'];
    }
    if(empty($_POST['mobile'])){
        $errors['mobile'] = "You have to enter mobile number to in mobile field";
    }elseif(!is_numeric($_POST['mobile'])){
        $errors['mobile'] = "mobile number have to be numeric value";
    } else{
        $mobile = $_POST['mobile'];
    }
    if(empty($_POST['company'])){
        $errors['company'] = "You have to enter company to in company field";
    }else{
        $company = $_POST['company'];
    }

}

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
    <style>
        .red{
           border: 2px solid #ff0000;
        }
        .green {
            border: 2px solid green;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>Please Enter the each field of this form </h1>
    <form method="post" action="">
        <div class="form-group">
            <label for="name">Name</label>
            <div class="alert-danger"> <?php if(isset($errors['name'])){ echo $errors['name']; } ?> </div>

            <input value="<?php if(isset($name)){echo $name;} ?>" type="text" name="name" class="form-control <?php if(isset($errors['name'])){ echo 'red'; } ?> <?php if(isset($name)){echo 'green';} ?> " id="name" placeholder="Enter name">
        </div>
        <div class="form-group">
            <label for="email">Email</label>
            <div class="alert-danger"> <?php if(isset($errors['email'])){ echo $errors['email']; } ?> </div>
            <input value="<?php if(isset($email)){echo $email;} ?>" type="text" name="email" class="form-control <?php if(isset($errors['email'])){ echo 'red'; } ?> <?php if(isset($email)){echo 'green';} ?>  " id="email" placeholder="Enter email">
        </div>
        <div class="form-group">
            <label for="mobile">Mobile</label>
            <div class="alert-danger"> <?php if(isset($errors['mobile'])){ echo $errors['mobile']; } ?> </div>
            <input value="<?php if(isset($mobile)){echo $mobile;} ?>" type="text" name="mobile" class="form-control <?php if(isset($errors['mobile'])){ echo 'red'; } ?>  <?php if(isset($mobile)){echo 'green';} ?> " id="mobile" placeholder="Enter mobile">
        </div>
        <div class="form-group">
            <label for="company">Company</label>
            <div class="alert-danger"> <?php if(isset($errors['company'])){ echo $errors['company']; } ?> </div>
            <input value="<?php if(isset($company)){echo $company;} ?>" type="text" name="company" class="form-control <?php if(isset($errors['company'])){ echo 'red'; } ?> <?php if(isset($company)){echo 'green';} ?>  " id="company" placeholder="Enter company">
        </div>
        <button type="submit" name="" class="btn btn-default">Submit</button>
    </form>
</div>

</body>
</html>

findings frequency number from a file by using own explode code and frequency code - modularity approach

in functions.php
===========================

<?php
//for exploding a string to array
function myExplode($string)
{
    $length = strlen($string);
    $word = "";
    $words = array();
    for ($i = 0; $i < $length; $i++) {
        if (ord($string[$i]) == 13) {
            continue;
        }
        if ($string[$i] == " " || $i == $length - 1 || $string[$i] == "\n") {

            if ($i == $length - 1) {
                $word .= $string[$i];
            }
            if(!empty($word)){
                $words[] = $word;
            }
            $word = "";
        } else {
            $word .= $string[$i];
        }
    }
    return $words;
}




//for determine frequency
function frequency($words){
    $frequency = array();

    foreach ($words as $word) {
        if(array_key_exists($word, $frequency)){
            $frequency[$word] ++;
        } else {
            $frequency[$word] = 1;
        }
    }

    return $frequency;
}


in file.php
================

<?php
require 'functions.php';
$pointer = fopen('php.txt', 'r');
$string = fread($pointer, filesize('php.txt'));
$words = myExplode($string);
$frequency = frequency($words);
print_r($frequency);

findings frequency number from a file by using own explode code and frequency code

<?php
$pointer = fopen('php.txt', 'r');
$string = fread($pointer, filesize('php.txt'));
$length = strlen($string);
$word = "";
$words = array();

for($i = 0; $i < $length; $i++){
    if(ord($string[$i]) == 13){
            continue;
        }
    if($string[$i] == " " || $i == $length - 1 || $string[$i] == "\n"){

        if($i == $length - 1){
            $word .= $string[$i];
        }
        if(!empty($word)){
            $words[] = $word;
        }
        $word = "";
    }else{
        $word .= $string[$i];
    }
}
//for determine frequency
$frequency = array();

foreach ($words as $word) {
    if(array_key_exists($word, $frequency)){
        $frequency[$word] ++;
    } else {
        $frequency[$word] = 1;
    }
}

foreach($frequency as $key => $value){
    echo "$key => $value <br>";
}

file writing and file appending || file handling in php

<?php
/*
 *file reading and file appending
 * */
$pointer = fopen('hello.txt', 'w');//mode is w means file open for writing purpose
fwrite($pointer, "Hello world\n"); //that will replace all existing sentence or word and write hello world in hello.txt file
fclose($pointer);

$pointer = fopen('hello.txt', 'a');//mode is a means file open for appending some line / word
fwrite($pointer, 'Hello world in another line');

file reading || file handling in php

<?php
/*
 *for reading only
 * */
$pointer = fopen('php.txt', 'r'); //first argument is file name, 2nd parameter is mode some modes are r, w, a, a+
//fgets() return one line from file keep cursor to end of the line
echo fgets($pointer) ;
echo '<br>';
echo fgets($pointer) ;
echo '<br>';
fclose($pointer);



$pointer2 = fopen('php.txt', 'r');
//fgets() return one line from file keep cursor to end of the line
echo fgets($pointer2) ;
echo '<br>';
echo fread($pointer2, filesize('php.txt'));//this line echoing everything (all word) from php.txt
echo '<br>';
fclose($pointer2);


$pointer3 = fopen('php.txt', 'r');
while(!feof($pointer3)){
    echo fgetc($pointer3) . '<br>';
}
fclose($pointer3);
//feof() is file-end-of-file, fgetc() is file-get-character();

Saturday, August 29, 2015

break the string into array manually using php like explode()

<?php
$string = "Hi how are you again bangladesh";
$length = strlen($string);
$words = array();
$new_string = "";
for($j = 0; $j < $length; $j++){
    if($string[$j] == " " || $j == $length-1){
        if($j == $length-1){
            $new_string .= $string[$j];
        }
        $words[] = $new_string;
        $new_string = "";
    }
    else{
        $new_string .= $string[$j];
    }
}

print_r($words);

Friday, August 28, 2015

findings frequency number from array using foreach loop

<?php
/**
 * Created by PhpStorm.
 * User: Polo-Dev
 * Date: 29-Aug-15
 * Time: 9:20 AM
 */
$string = "hi how are you hi you";
$words = explode(" ", $string);
$new_array = array();

foreach ($words as $word) {
   if(array_key_exists($word, $new_array)){
       $new_array[$word] ++;
   } else {
       $new_array[$word] = 1;
   }
}

foreach($new_array as $key => $value){
    echo "$key => $value <br>";
}

findings frequency number from array using for loop

<?php
/**
 * Created by PhpStorm.
 * User: Polo-Dev
 * Date: 29-Aug-15
 * Time: 9:20 AM
 */
$string = "hi how are you hi you";
$array = explode(" ", $string);
$length = count($array);
$new_array = array();
for($i = 0; $i < $length; $i++){
    if(array_key_exists($array[$i], $new_array)){
        $new_array[$array[$i]] ++;
    }else{

        $new_array[$array[$i]] = 1;
    }
}
foreach($new_array as $key => $value){
    echo "$key => $value <br>";
}

page portion change in onclick without loading page using pure JavaScript (like bootstrap tabs and pills)

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<ul>
    <li><a onclick="event.preventDefault(); profile_function()" href="#">profile</a></li>
    <li><a onclick="event.preventDefault(); contact_function()" href="#">contact</a></li>
    <li><a onclick="event.preventDefault(); portfolio_function()" href="#">portfolio</a></li>

</ul>
<div id="main">
    <div id="profile">
        <h1>This is the profile section</h1>
    </div>
    <div id="contact">
        <h1>This is the contact section</h1>
    </div>
    <div id="portfolio">
        <h1>This is the portfolio section</h1>
    </div>
    <script>
        var main = document.getElementById('main');
        var profile = document.getElementById('profile');
        var contact = document.getElementById('contact');
        var portfolio = document.getElementById('portfolio');
        main.removeChild(profile);
        main.removeChild(contact);
        main.removeChild(portfolio);
        main.appendChild(profile);
        presentNode = "profile";
        previousNode = "profile";

        function adjust() {
           switch (previousNode){
               case "profile":
                   main.removeChild(profile);
                   break;
               case "contact":
                   main.removeChild(contact);
                   break;
               case "portfolio":
                   main.removeChild(portfolio);
                   break;
               default :
                   break;
           }
            previousNode = presentNode;
            switch (presentNode){
                case "profile":
                    main.appendChild(profile);
                    break;
                case "contact":
                    main.appendChild(contact);
                    break;
                case "portfolio":
                    main.appendChild(portfolio);
                    break;
                default :
                    break;
            }
        }

        function profile_function() {
            presentNode = "profile";
            adjust();
        }
        function contact_function() {
            presentNode = "contact";
            adjust();
        }
        function portfolio_function() {
            presentNode = "portfolio";
            adjust();
        }
    </script>
</div>
</body>
</html>

array in descending order in php

<?php
$array = [2, 5, 7, 99, 90, 42, 88, 8, 9, 0, 56];
$count = count($array);
for($i = 0; $i < $count; $i++){
    for($j = 0; $j < $count-1; $j++){
        if($array[$j] < $array[$j+1]){
           $temp = $array[$j+1];
            $array[$j+1]= $array[$j];
            $array[$j] = $temp;
        }
    }
}

for($k = 0; $k < $count; $k++){
    echo $array[$k] . '<br>';
}

sorting ascending order manually in php

<?php
$array = [2, 5, 7, 99, 90, 42, 88, 8, 9, 0, 56];
$count = count($array);
for($i = 0; $i < $count; $i++){
    for($j = 0; $j < $count-1; $j++){
        if($array[$j] > $array[$j+1]){
           $temp = $array[$j+1];
            $array[$j+1]= $array[$j];
            $array[$j] = $temp;
        }
    }
}

for($k = 0; $k < $count; $k++){
    echo $array[$k] . '<br>';
}

PHP echo vs print Statements

echo and print are more or less the same. They are both used to output data to the screen.

The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple parameters (although such usage is rare) while print can take one argument. echo is marginally faster than print.
(from w3school)

PHP logical expression and php logic building with increments, decrements, while loop, if else statement

Question 1:

<?php 
$x = 0; 
$y = $x--;
$z = --$x;
echo “x=$x,y=$y,z=$z”
?>
a. x = 0 , y = 0 , z = -1
b. x = -1 , y = -1 , z = -2
c. x = -2 , y = 0 , z = -2
d. x = 0 , y = 0 , z = -2


Question 2:

<?php 
$x = 0; 
$y = --$x  + 1 ;
$z = --$y -1;
echo “x=$x,y=$y,z=$z”
?>
e. x = 0 , y = 0 , z = -2
f. x = -1 , y = -1 , z = -2
g. x = -2 , y = 0 , z = -2
h. x = 0 , y = 0 , z = -2


Question 3:

<?php 
$x = 0; 
$y = $x--;
$z = --$y;
echo “x=$x,y=$y,z=$z”
?>
i. x = -1, y = -1, z = -1
j. x = -1, y = -1, z = -2
k. x = -1, y = -2, z = -2
l. x = 0, y = 0, z = -2

Question 4:

<?php 
$x = 0; 
$y = $x - 1;
$x = $y--;
$z = --$x;
echo “x=$x,y=$y,z=$z”
?>
m. x = 0, y = 0, z = -1
n. x = -1, y = -1, z = -2
o. x = -2, y = 0, z = -2
p. x = -2, y = -2, z = -2

Question 5:

<?php 
$x = -1; 
$y = $x + 1;
$z = ++$x + $y ;
echo “x=$x,y=$y,z=$z”
?>
q. x = 0, y = 0, z = 0
r. x = -1, y = -1, z = -2
s. x = -2, y = 0, z = -2
t. x = 0, y = 0, z = -2

Question 6:

<?php 
$x = 0; 
$y = $x--;
$z = ($x++) + $y;
echo “x=$x,y=$y,z=$z”
?>
u. x = 0, y = 0, z = -1
v. x = -1, y = -1, z = -2
w. x = -2, y = 0, z = -2
x. x = 0, y = 0, z = -2

Question 7:

<?php 
$x = 0; 
$y = (--$x) + 1;
$z = --$x + (++$y);
echo “x=$x,y=$y,z=$z”
?>
y. x = 0, y = 0, z = -1
z. x = -1, y = -1, z = -2
aa. x = -2, y = 1, z = -1
bb. x = 0, y = 1, z = -2


Question 8:

<?php 
$x = 0; 
$y = $x--;
$y = --$x;
$y--;
$z = --$x;
echo “x=$x,y=$y,z=$z”
?>
cc. x = -3, y = -2, z = -3
dd. x = -3, y = -1, z = -3
ee. x = -2, y = -2, z = -2
ff. x = -3, y = -3, z = -3

Question 9:

<?php 
$x = 0; 
$y = $x + 1;
$z = (--$x)  + ($y++);
echo “x=$x,y=$y,z=$z”
?>
gg. x = -1, y = 2, z = -1
hh. x = -1, y = 2 , z = 0
ii. x = -2, y = 1, z = -2
jj. x = -1, y = 2, z = -2

Question 10:
$i = 0;
while($i == 0){
   echo $i;
   $i++;
}
/*
*out put 0
*/

Question 11:
$i = 5;
while($i--){
echo $i . ", ";
}
/*
*out put  4, 3, 2, 1, 0,
*/

Question 12:
$x = -1;
if($x++){
echo "if";
} else {
echo "else";
}
/*
*out put if 
*/

Question 13:
$x = -1;
if(++$x){
echo "if";
} else {
echo "else";
}
/*
*out put else 
*/

programming glossary

lazy evaluation,
DRY - don't repeat yourself

natural number, whole number & integer

1  -to-  infinity = natural number
0  -to-  infinity = whole number
-infinity  -to-   +infinity = integer

How to reverse a string

//how to reverse a string
$string = "FTFL!!";
$count = strlen($string) - 1;;
for($i = $count; $i >= 0; $i--){
  echo $string[$i];
}

sum of square of natural number

/*
 * sum of square of natural number
 * */
$n = 5;
$sum = 0;
$i = 1;
while($n){
   $i = $n;
   $sum += $i ** 2;
   $n--;
}
echo $sum;


/*
 * sum of square of natural number in another way
 * */
$n = 5;
$sum = 0;
$i = 1;
while($n){
   $sum += ($n * $n);
   $n--;
}

echo $sum;

Variable swap

/*
 * variable swap
 * */
$a = 7;
$b = 8;
$i = $a;
$a = $b;
$b = $i;
echo $a , $b;

/*
 * another way to variable swap
 * */
$a = 25;
$b = 30;
$a = $a + $b;
$b = $a - $b;
$a = $a - $b;
echo $a, $b;

How to get min number from an array

/*
 * How to get min number from a array
 * */
$numbers = array(9, 0, 1, 77, 4, 5, -1, 10);

$min_number = $numbers[0];
$length = count($numbers);//in order to save time we initial length outside of the loops
for($i = 0; $i < $length; $i++){
   if ($min_number > $numbers[$i]){
       $min_number = $numbers[$i];
   }
}
echo "Min number is the array is ", $min_number;

how to get max number from an array

/*
* How to get max number from a array
* */
$numbers = array(9, 0, 1, 77, 4, 5, -1, 10);

$max_number = $numbers[0];
$length = count($numbers);//in order to save time we initial length outside of the loops
for($i = 0; $i < $length; $i++){
   if ($max_number < $numbers[$i]){
       $max_number = $numbers[$i];
   }
}
echo "Max number is the array is ", $max_number;

converting for loop to while loop

/*
 *converting for loop to while loop
 * */
for($i = 0; $i < 5; $i++){
   echo $i;
}

$i = 0;
while($i < 5){
   echo $i;
   $i++;
}

Mathematical approach to determine Sum of natural number and sum of odd natural number

sum of natural number
======================================
s  = 1 + 2     + 3     + 4     + . . . . . . + n
s  = n + (n-1) + (n-2) + (n-3) + (n-4) + ..
2s = (n+1) + (n+1) + (n+1) + (n+1) + (n+1) . . . . 
s  = ((n+1) + (n+1) + (n+1) + (n+1) + (n+1) . . . . ) / 2
s = (n(n+1)) / 2

sum of odd natural number
======================================
s = 1      + 3      + 5        + 7        +..... + (2n-1)
s = (2n-1) + (2n-3) + (2n - 5) + (2n - 7) + ....
2s = 2n + 2n + 2n + ......
2s = n * 2n 
s = n * 2n / 2
s = n * n
s = n**2

Sum of natural number in PHP

Sum of odd natural number in PHP


Sum of natural number in PHP

/*
 * sum of natural number
 * */
$x = 8;
$result = $x;
while($x--){
  $result += $x;
}
echo $result;
/*
 * sum of natural number in another way
 * */
$x = 8;
$result = 0;
while($x){
  $result += $x--;
}
echo $result;

Sum of odd natural number in PHP

/*
 *
 * sum of odd natural number
 * */
$x = 3;
$result = 0;
while($x){
  $result =$result + (2 * $x--)-1;
}
echo $result;

/*
 *
 * sum of odd natural number in another way
 * */
$n = 4;
$sum = 0;
$i = 1;
while($n--){
   $sum += $i;
   $i = $i + 2;
}
echo $sum;

Tuesday, August 25, 2015

example of static scope in php

static means it's initiated only one time
output will be :
value of x inside sample is 1
value of x inside sample is 2
value of x inside sample is 3



<?php

function scope(){
    echo "Value of x is ", $x;
}
//static means it's initiated only once
//first it's initiated by 0
function sample() {
    static $x = 0;
    $x = $x + 1;
    echo "value of x inside sample is ", $x, "<br>";
}

sample();
sample();
sample();

Creating a Basic Calculator using PHP

<?php
$errors = array();
if ( $_SERVER['REQUEST_METHOD'] == 'POST'){

    if(!isset($_POST['value1']) || trim($_POST['value1']) == ""){
        $errors[] = "You must enter first Number";
    } elseif(!is_numeric($_POST['value1'])) {
        $errors[] = "You must put numeric value in first number field";
    }else{
        $value1 =$_POST['value1'];
    }


    if(!isset($_POST['value2']) || trim($_POST['value1']) == ""){
        $errors[] = "You must enter Second Number";
    } elseif(!is_numeric($_POST['value2'])) {
        $errors[] = "You must put numeric value in 2nd number field";
    }else{
        $value2 =$_POST['value2'];
    }

    if(empty($_POST['value3'])){
        $errors[] = "You must enter a operator";
    }elseif( $_POST['value2'] == 0 && $_POST['value3'] == "/") {
        $errors[] = "Second Number can't be zero in division";
    } else {
        $operator =$_POST['value3'];
    }

    if(empty($errors)){
        if($operator == "+"){
            $result = $value1 + $value2;
        } elseif($operator == "-"){
            $result = $value1 - $value2;
        } elseif($operator == "*"){
            $result = $value1 * $value2;
        } elseif($operator == "/"){
            $result = $value1 / $value2;
        }else{
            $errors[] = "Currently our supporting operator is +-*/";
        }
    }
}
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        body{
            font-family: arial, sans-serif;
        }
        label {
            display: block;
        }
    </style>
</head>
<body>
<form action="" method="post">
    <label>First Number:</label>  <input type="text" name="value1" placeholder="Number one"><br><br>
    <label for="">Second Number:</label> <input type="text" name="value2" placeholder="Number Two"><br><br>
    <label for="">Operator(currently supported operator is +-*/):</label><input type="text" name="value3" placeholder="operator"><br><br>
    <input type="submit" name="submit" id="submit">
</form>
<div id="result">
    <?php
        if(isset($result)){
            echo $result;
        }
        if(isset($errors)){
            foreach($errors as $error){
                echo $error . "<br>";
            }

        }
    ?>
</div>
</body>
</html>

Common Document object model traversing

document.createElement() Creates an Element node
document.createTextNode() Creates a Text node

element.appendChild("new node") Adds a new child node, to an element, as the last child node
element.insertBefore("new node", "Which node before") Inserts a new child node before a specified, existing, child node

element.removeChild("existing node") Removes a child node from an element
element.replaceChild("new node", "Which existing node will be replace") Replaces a child node in an element

to learn more: http://www.w3schools.com/jsref/dom_obj_document.asp

Dom Traversing
================================================
element.parentNode Returns the parent node of an element
element.parentElement Returns the parent element node of an element

element.childNodes Returns a collection of an elements child nodes (including text and comment nodes)
element.childNodes[i] Returns an element child nodes (including text and comment nodes)


element.firstChild Returns the first child node of an element
element.firstElementChild Returns the first child element of an element

element.lastChild Returns the last child node of an element
element.lastElementChild Returns the last child element of an element

element.nextSibling Returns the next node at the same node tree level
element.nextElementSibling Returns the next element at the same node tree level

element.previousSibling Returns the previous node at the same node tree level
element.previousElementSibling Returns the previous element at the same node tree level

to learn more: http://www.w3schools.com/jsref/dom_obj_all.asp



Image change on onClick event and slideshow image - basic image slide with minimum effort

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Image change using on click method</title>
    <style>
        #img_div {
            height: 400px;
            overflow: hidden;
        }
        .profile img{
            height: 100%;
            width: auto;
        }
    </style>
</head>
<body>
<div class="profile">
    <div id="img_div"> </div> <br>
    <button id="prev" class="btn btn-success" onclick="prev_img()">Prev</button>
    <button id="next" class="btn btn-success" onclick="next_img()">Next</button>
    <br> <br>
</div>

<script>
//    img path variable will define path of individual image;
    var img_path = ["images/1.jpg", "images/2.jpg", "images/3.jpg", "images/4.jpg", "images/5.jpg", "images/6.jpg"]
    var img_div =  document.getElementById('img_div');
    var img_tag = document.createElement("img");
    img_tag.className = "img-responsive";
    img_div.appendChild(img_tag);
    var index = 0;
    img_tag.src = img_path[index];
    var prev = document.getElementById('prev');
    var next = document.getElementById('next');
    if(index == 0){
        prev.disabled = true;
    }
    function next_img(){
        index++;
        if(index < img_path.length){
            img_tag.src = img_path[index];
            prev.disabled = false;
        } else{
            index = 0;
            img_tag.src = img_path[index];
            prev.disabled = true;
        }
    }
        setInterval(next_img, 2000);

    function prev_img(){
        index--;
        img_tag.src = img_path[index];
        if(index == 0){
            prev.disabled = true;
        }
    }
</script>
</body>
</html>

Monday, August 24, 2015

Creating Pyramid in JavaScript using for loop

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>pyramid</title>
</head>
<body>
<input type="text" name="" id="number"> <button onclick="pyramid()">Click to see your number pyramid</button>
<br>
<div id="result"></div>

<script>
    function pyramid(){
        var number = document.getElementById('number').value;
        var div_result = document.getElementById('result');
        var result = "";
        for(var i = 1; i <= number; i++){
            var star = "";
            for(var j = 0; j < number - i; j++){
                star = star + '&#160;&#160;';
            }
            for (var k = 0; k < 2 * i -1; k++){
                star = star + "*"
            }
            result  += '<br>' +  star;
        }
        div_result.innerHTML = result;
        console.log(number);
    }

</script>
</body>
</html>

Sunday, August 23, 2015

string matching manual function - result view in console

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Match</title>
</head>
<body>

<script>
    var str_one = "hello world";
    var str_two = "wor";
    function mySearch(str_one, str_two){
       for(var i = 0; i < str_one.length; i++){
           for (var j = 0; j < str_two.length; j++){
               if(str_one.charAt(i+j) == str_two.charAt(j)){
                   if(j == str_two.length -1){
                       return i;
                   }
               }else{
                   break;
               }
           }
       }
    }
    console.log(mySearch(str_one, str_two));


</script>
</body>
</html>

css snippet for blogger code highlighting

code, .code {     display: block;     background: beige;     padding: 10px;     margin: 8px 15px; }