Thursday 17 November 2016

Enable only holidays and weekends in JQuery Datepicker

We can use JQuery datepicker's beforeshowday() to enable only weekends and off days (i.e. specific festival days).

As per JQuery UI's documentation beforeShowDay() takes date as parameter and must return an array


[0]: true/false indicating whether or not this date is selectable

[1]: a CSS class name to add to the date's cell or "" for the default presentation

[2]: an optional popup tooltip for this date

The function is called for each day in the datepicker before it is displayed.

So here's the example where I've written a code to disable all the weekdays excluding specific days such as festivals and weekends.

$(document).ready(function(){
 $("#datepicker").datepicker({
   changeMonth: true,
        changeYear: true,
   beforeShowDay:function(dt){
     var enableDay = ["15-8-2016","5-9-2016","11-10-2016","31-10-2016","10-11-2016", "26-12-2016"];       
        var dmy = dt.getDate() + "-" + (dt.getMonth() + 1) + "-" + dt.getFullYear();
     if ($.inArray(dmy, enableDay) > -1){
      return [true, ""];
     }else{
      return [dt.getDay() == 0 || dt.getDay() == 6];
     }           
    }
  });
});

Demo


Hope it'll help :)

You can find more about disabling dates in JQuery here: How to Disable Dates in Jquery DatePicker – A Short Guide

Friday 4 November 2016

JavaScript Logical Examples and Programs

Exlained Answers of JavaScript Web Quiz by David Shariff
Try the most advance and complex JavaScript Quiz implemented by David Shariff
Compare two arrays and find difference between them?

<script type="text/javascript">
    var array1 = [1, 2, 3, 4, 5, 6];
    var array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];

    var result = [];

    /***********Method 1************/
    var count = 0;
    $.grep(array2, function (index) {
        console.log(index);
        if ($.inArray(index, array1) == -1)
            result.push(index);
        count++;
    });
    console.log(" the difference is " + result);


/************Method 2**************/
    var len = array2.length;
    for (var i = 0; i <= len - 1; i++) {
        if (array1.indexOf(array2[i]) == -1) {
            result.push(array2[i]);
        }
    }
    console.log(" the difference is " + result);


/************Method 3**************/
    var diff = $(array2).not(array1).get();
    alert(diff);
</script>
Find the second largest element from an array in javascript?
function findSecondLargest(arr) {
    // Sort the array in descending order
    arr.sort((a, b) => b - a);
    
    // If the array has less than two elements, return undefined
    if (arr.length < 2) {
        return undefined;
    }
    
    // Return the second element
    return arr[1];
}

// Example usage
const array = [10, 5, 20, 15];
const secondLargest = findSecondLargest(array);
console.log('Second largest element:', secondLargest); // Output: 15

var secondMax = function (arr) {     
    var max = Math.max.apply(null, arr); // get the max of the array
    arr.splice(arr.indexOf(max), 1); // remove max from the array
    return Math.max.apply(null, arr); // get the 2nd max
};

var arr = [20, 120, 111, 215, 54, 78]; // use int arrays
var max2 = secondMax(arr);
alert(max2);
Find smallest element from array without using any built-in function
// Find smallest element from array without using any built-in function

var arr = [7,2,4,3,1,6,5,8];
var minEle = arr[0];

for(var i=1; i<arr.length; i++){
    if(arr[i] < minEle ){
        minEle = arr[i];   
    }
}

alert(minEle);
Create a for loop that iterates up to 100 while outputting "fizz" at multiples of 3, "buzz" at multiples of 5 and "fizzbuzz" at multiples of 3 and 5

<script type="text/javascript">
    for (var i = 1; i <= 100; i++) {
        if (i % 3 === 0 && i % 5 === 0)
            console.log(i + "== fizzbuzz");
        else if (i % 3 === 0)
            console.log(i + "== fizz");
        else if (i % 5 === 0)
            console.log(i + "== buzz");
    }
</script>

List Prime Numbers between 1 to 100 in JavaScript
for (var p = 2; p <= 100; p++) {
    var isPrime = true;

    // Check divisibility from 2 to square root of p
    for (var i = 2; i <= Math.sqrt(p); i++) {
        if (p % i === 0) {
            isPrime = false;
            break; // Exit the loop if p is not prime
        }
    }

    // If isPrime is still true, p is a prime number
    if (isPrime) {
        console.log(p);
    }
}

- Iterate through each number p from 2 to 100.

- For each p, we set a flag isPrime to true initially, assuming it's prime.

- We then check divisibility of p with all numbers from 2 to the square root of p.

- If p is divisible by any number in this range, we set isPrime to false and break out of the loop.

- If isPrime is still true after checking all numbers, p is a prime number, and we log it to the console.

List Fibbonaci upto 100 in JavaScript

<script type="text/javascript">
    //The Fibonacci sequence is a series where the next term is the sum of pervious two terms. 
    //The first two terms of the Fibonacci sequence is 0 followed by 1.
    var t1 = 0, t2 = 1, nextTerm = 0;
    console.log(t1); // print 0 as it is
    console.log(t2); // print 1 as it is
    for (var i = 0; i <= 100; i++) {
        nextTerm = t1 + t2;
        t1 = t2;
        t2 = nextTerm;
        console.log(nextTerm);
    }
</script>

How to add something in the beginning and end of an array

var array = ['Java','PHP','SQL','HTML'];

// add "Angular" in the beginning

array.unshift('Angular');

// add "React" in the end

array.push('React');

alert(array);

// OR ES6 Way using spread operator
array = ["Angular",...array,"React"];
alert(array);

How to find duplicate elements from an Array
//var myArr = [ 'x','y','a','x','b','b','a','c','b','k','b' ];
var myArr = [ 6, 4, 5, 5, 5, 2, 1, 3, 1, 1, 2, 1, 3, 4, 7 ];

// you must need to sort the array first
var myArrSorted = myArr.sort();

// in filter function
// 1st parameter is each element from an array
// 2nd parameter is index of each element
// 3rd parameters is an array itself
// so we need to check if each element with its next element and 
// return only if element is not duplicate
var result = myArrSorted.filter( function(element,index,arrayitself){ 
  if(element !== arrayitself[index+1]){
 return element;
  }
});

console.log(result);

Remove adjacent duplicate characters from string
// remove adjacent duplicate characters from string
// ex:
// input: AAABBCDDDAAD
// output: ABCDAD

let string = "AAABBCDDDAAD";
let len = string.length;
let newStr = "";

for(let i=1; i<len+1; i++){
 if(string[i] != string[i-1]){
   newStr += string[i-1];
  }
}

console.log(newStr)

Sort an Array without using any built-in JavaScript function.
let arr = [5,2,4,10,3,1,5];
//let arr = ['d','a','c','b','e'];

let sortArr = function(arr){
  flag = true;
  while(flag){
    flag = false;  
    for(let i=0;i<arr.length;i++){
      if(arr[i] > arr[i+1]){
        let temp = arr[i];
        arr[i] = arr[i+1];
        arr[i+1] = temp;                  
        flag = true;
      }

    }
  }
  return arr;
}

sortArr(arr);

Thursday 3 November 2016

Explained Answers of JavaScript Web Quiz by David Shariff


Try the most advance and complex JavaScript Quiz implemented by David Shariff.

Here is the link: David Shariff JavaScript Quiz

And here is the Youtube Channel link where "Codedamn" attempted and explained all the questions available in above quiz.