Recent

Tuesday, January 4, 2022

January 04, 2022

Date Format Validation in Javascript

 


There’re several different ways to handle Date values through the script, all depends on the exact need. In some cases, there’s a need to enter a date value manually, as text, yet to force it to be in a specific valid date format.


Below is the javascript code to valdate date with given format. Add this code inside the script tag


function ValidateDate(dt, format) {

    var isValid = true;

    if(dt.length != format.length) {
        isValid = false;
    }

    format = format.replace(/\ /g, "/").toLowerCase();
    dt = dt.replace(/\ /g, "/");
    var dateFormat = dt;
    //Get Day Value
    var day = 0;
    if(format.indexOf("dd") > -1) {
        var index = format.indexOf("dd"),
        day = dt.substring(index * 1 + 2);
      dateFormat=  dateFormat.replaceAt(index, "dd");

    } else if(format.indexOf("d") > -1) {
        var index = format.indexOf("dd"),
        day = dt.substring(inde, index * 1 + 1);
       dateFormat=  dateFormat.replaceAt(index, "d");
    } else {
        isValid = false;
    }

    //Get Month Value
    var month = 0;
    if(format.indexOf("mmm") > -1) {
        var index = format.indexOf("mmm"),
        month = dt.substring(index, index * 1 + 3);
      dateFormat=   dateFormat.replaceAt(index, "mmm");
    } else if(format.indexOf("mm") > -1) {
        var index = format.indexOf("mm"),
        month = dt.substring(index, index * 1 + 2);
       dateFormat=  dateFormat.replaceAt(index, "mm");
    }
    else {
        isValid = false;
    }

    var year = 0;
    if(format.indexOf("yyyy") > -1) {
        var index = format.indexOf("yyyy"),
        year = dt.substring(index, index * 1 + 4);
       dateFormat=  dateFormat.replaceAt(index, "yyyy");
    } else if(format.indexOf("yy") > -1) {
        var index = format.indexOf("yy"),
        year = dt.substring(index, index * 1 + 2);
      dateFormat=   dateFormat.replaceAt(index, "yy");
    }
    else {
        isValid = false;
    }

    if(dateFormat != format)
    {
        isValid = false;
    }

    console.log(day);
    console.log(month);
    console.log(year);
	console.log(isValid);

}
String.prototype.replaceAt = function(index, replacement) {
    return this.substr(0, index) + replacement + this.substr(index + replacement.length);
}

ValidateDate("12-07-2017", "MM-DD-YYYY");
ValidateDate("12-07-2017", "DD/MM/YYYY");
ValidateDate("2017-07-12", "YYYY-MM-DD");
 
January 04, 2022

Heart shape using JavaScript and canvas

 




HTML

<canvas id="canvas" width="525" height="525"></canvas>

Javascript

var heart = [65, 66, 67, 72, 73, 74, 84, 85, 86, 87, 88, 91, 92, 93, 94, 95, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 266, 267, 268, 269, 270, 271, 272, 273, 287, 288, 289, 290, 291, 292, 308, 309, 310, 311, 329, 330];

var nums;
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');

Array.matrix = function(numrows, numcols, initial) {
    var arr = [];
    for(var i = 0; i < numrows; ++i) {
        var columns = [];
        for(var j = 0; j < numcols; ++j) {
            columns[j] = initial;
        }
        arr[i] = columns;
    }
    return arr;
}

function shuffleArray(array) {
    for(let i = array.length - 1; i > 0; i--) {
        let j = Math.floor(Math.random() * (i + 1));
        [array[i], array[j]] = [array[j], array[i]];
    }
    return array;
}
function animate(xPos, yPos, count) {
    var r = randomNumber(64, 254 - 64);
    var g = randomNumber(128, 255);
    var b = randomNumber(128, 255);
    var alpha = randomNumber(55, 85);
    //For heart Sharpe
    if(inArray(count, heart)) {
        r = randomNumber(128 + 64, 255);
        g = randomNumber(0, 63);
        b = randomNumber(0, 63);
        alpha = randomNumber(70, 100);
    }
    context.fillStyle = 'rgba(' + r + ',' + g + ',' + b + ',' + alpha + ')';
    context.fillRect(xPos, yPos, 25, 25);
}
function randomNumber(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
}
draw();
function draw() {
    if(canvas.getContext) {
        nums = Array.matrix(400, 3, 0);
        var x = 0;
        var y = 0;
        var count = 0;
        for(var j = 0; j < 20; j++) {
            for(var i = 0; i < 20; i++) {
                nums[count][0] = x;
                nums[count][1] = y;
                nums[count][2] = count;
                count += 1;
                x += 26;
            }
            x = 0;
            y += 26;
        }
        nums = shuffleArray(nums);
        for(var j = 0; j < nums.length; j++) {
            doSetTimeout(j);
        }
    }
}
function doSetTimeout(i) {
    setTimeout(function() { animate(nums[i][0], nums[i][1], nums[i][2]); }, 10 * i);
}
function animate(xPos, yPos, count) {
    var r = randomNumber(64, 255 - 64);
    var g = randomNumber(128, 255);
    var b = randomNumber(128, 255);
    var alpha = randomNumber(55, 85);
    //For heart Sharpe
    if(inArray(count, heart)) {
        r = randomNumber(128 + 64, 255);
        g = randomNumber(0, 63);
        b = randomNumber(0, 63);
        alpha = randomNumber(70, 100);
    } else {
        context.globalAlpha = 0.6;
    }

    context.fillStyle = 'rgba(' + r + ',' + g + ',' + b + ',' + alpha + ')';
    fillRoundedRect(xPos, yPos, 25, 25, 4);
}
function fillRoundedRect(x, y, w, h, r) {

    context.beginPath();
    context
    context.moveTo(x + r, y);
    context
    context.lineTo(x + w - r, y);
    context
    context.quadraticCurveTo(x + w, y, x + w, y + r);
    context
    context.lineTo(x + w, y + h - r);
    context
    context.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
    context
    context.lineTo(x + r, y + h);
    context
    context.quadraticCurveTo(x, y + h, x, y + h - r);
    context
    context.lineTo(x, y + r);
    context
    context.quadraticCurveTo(x, y, x + r, y);
    context
    context.fill();

}
function inArray(value, array) {
    var count = array.length;
    for(var i = 0; i < count; i++) {
        if(array[i] === value) { return true; }
    }
    return false;
}

Download source code from github Convas Love Heart


DONT CLICK THIS - https://bit.ly/3F3Glqn

January 04, 2022

FizzBuzz Problem Solution with C#


  The "Fizz-Buzz test" is an interview question designed to help filter out the 99.5% of programming job candidates who can't seem to program their way out of a wet paper bag. The text of the programming assignment is as follows:

"Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”."

Method 1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 100; i++)
            {
                if (i % 3 == 0 && i % 5 == 0)
                {
                    Console.WriteLine("FizzBuzz");
                }
                else if (i % 3 == 0)
                {
                    Console.WriteLine("Fizz");
                }
                else if (i % 5 == 0)
                {
                    Console.WriteLine("Buzz");
                }
                else
                {
                    Console.WriteLine(i);
                }
                
            }
            Console.ReadLine();
        }
    }
}

Method 2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 100; i++)
            {
                string str = "";
                if (i % 3 == 0)
                {
                    str += "Fizz";
                }
                if (i % 5 == 0)
                {
                    str += "Buzz";
                }
                if (str.Length == 0)
                {
                    str = i.ToString();
                }
                Console.WriteLine(str);

            }
            Console.ReadLine();
        }
    }
}

Method 3 (Using Linq)

using System;
using System.Linq;

namespace fizzbuzz
{
    class Program
    {
        static void Main(string[] args)
        {
            Enumerable.Range(1,100).ToList()
                .ForEach(x =>
                {
                    Console.WriteLine("{0}{1}{2}", 
                        x % 3 == 0 ? "Fizz" : "",
                        x% 5 == 0 ? "Buzz" : "",
                        x % 3 != 0 && x % 5 != 0 ? x.ToString() : "");
                });
        }
    }
}

Thursday, December 30, 2021

December 30, 2021

C# Hello World - Your First C# Program

 C# Hello World - Your First C# Program


The “Hello World!” program is often the first program we see when we dive into a new language. It simply prints Hello World! on the output screen.


  
using System;

public class HelloWorld
{
    public static void Main(string[] args)
    {
        Console.WriteLine ("Hello Mono World");
    }
}
  

Output

Hello World!

Wednesday, December 29, 2021

December 29, 2021

C# Program to Check Whether the Entered Year is a Leap Year or Not





This is a C# Program to check whether the entered year is a leap year or not.

Problem Description

This C# Program Checks Whether the Entered Year is a Leap Year or Not.

Problem Solution

When A year is divided by 4. If the remainder becomes 0 then the year is called a leap year.

Program/Source Code

Here is the source code of the C# Program to Check Whether the Entered Year is a Leap Year or Not. The C# program is successfully compiled and executed with Microsoft Visual Studio. 

  
using System;
public class Program
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Enter Year : ");  
        int Year = int.Parse(Console.ReadLine());  
        if (((Year % 4 == 0) && (Year % 100 != 0)) || (Year % 400 == 0)) 
            Console.WriteLine("{0} is a Leap Year.", Year);  
        else 
            Console.WriteLine("{0} is not a Leap Year.", Year);  
        
        Console.ReadLine();  
    }
}
  
December 29, 2021

C# Program to Swap 2 Numbers



This is a C# Program to swap 2 numbers.

Problem Description

This C# Program Swaps 2 Numbers.

Problem Solution

It obtains two numbers from the user and swaps the numbers using a temporary variable.

Program/Source Code

Here is the source code of the C# program that swaps two numbers. The C# program is successfully compiled and executed with Microsoft Visual Studio. 


  
  /*
 * C# Program to Swap two Numbers
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1, num2, temp;
            Console.Write("\nEnter the First Number : ");
            num1 = int.Parse(Console.ReadLine());
            Console.Write("\nEnter the Second Number : ");
            num2 = int.Parse(Console.ReadLine());
            temp = num1;
            num1 = num2;
            num2 = temp;
            Console.Write("\nAfter Swapping : ");
            Console.Write("\nFirst Number : "+ num1);
            Console.Write("\nSecond Number : "+ num2);
            Console.Read();
        }
    }
}
  

Tuesday, December 28, 2021

December 28, 2021

C# Program to Check whether the Entered Number is Even or Odd



 This is a C# Program to check whether the entered number is even or odd.

Problem Description

This C# Program checks if a given integer is Odd or Even.

Problem Solution

Here if a given number is divisible by 2 with the remainder 0 then the number is an Even number. If the number is not divisible by 2 then that number will be an Odd number.

Program/Source Code

Here is source code of the C# program which checks a given integer is odd or even. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.


/*
 * C# Program to Check whether the Entered Number is Even or Odd
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            int i;
            Console.Write("Enter a Number : ");
            i = int.Parse(Console.ReadLine());
            if (i % 2 == 0)
            {
                Console.Write("Entered Number is an Even Number");
                Console.Read();
            }
            else
            {
                Console.Write("Entered Number is an Odd Number");
                Console.Read();
            }
        }
    }
}

DONT CLICK THIS - https://bit.ly/3F3Glqn