Skip to content

Instantly share code, notes, and snippets.

View vasanthv's full-sized avatar

vasanth vasanthv

View GitHub Profile
@vasanthv
vasanthv / datetimestring.js
Last active October 22, 2022 02:26
Format a date string
/*
This function changes the datestring to the following format
Jan 29 2018 - 09:51 AM
*/
function formatDate(dateString){
const date = new Date(dateString);
const hours = date.getHours() > 12 ? date.getHours() - 12 : date.getHours();
return [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ][date.getMonth()] + ' '
+ (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
+ date.getFullYear() + ' - '
@vasanthv
vasanthv / getmonthname.js
Created May 27, 2016 16:42
Get month name from date object
var date = new Date();
var months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
console.log("The current month is " + months[date.getMonth()]);
function timeAgo(time){
var units = [
{ name: "second", limit: 60, in_seconds: 1 },
{ name: "minute", limit: 3600, in_seconds: 60 },
{ name: "hour", limit: 86400, in_seconds: 3600 },
{ name: "day", limit: 604800, in_seconds: 86400 },
{ name: "week", limit: 2629743, in_seconds: 604800 },
{ name: "month", limit: 31556926, in_seconds: 2629743 },
{ name: "year", limit: null, in_seconds: 31556926 }
];
function getRandom(arr, n) {
var result = new Array(n),
len = arr.length,
taken = new Array(len);
if (n > len)
throw new RangeError("getRandom: more elements taken than available");
while (n--) {
var x = Math.floor(Math.random() * len);
result[n] = arr[x in taken ? taken[x] : x];
taken[x] = --len in taken ? taken[len] : len;
@vasanthv
vasanthv / all-combinations.js
Created March 12, 2018 11:46
The function returns all possible combinations of a given number represented as array.
//Based on https://stackoverflow.com/a/46880431/607608
function getComb(set, val) {
const res = [];
function comb(set, k, partial, pref) {
if (!partial) partial = [];
for (const element in set) {
if (k > 1) {
const set_copy = set.slice();
set_copy.splice(element, 1);
const random = (i,j) => (Math.floor(Math.random() * (j - i)) + i);
@vasanthv
vasanthv / get-combinations.js
Created May 7, 2018 07:40
Get combinations of all elements in an array with optional length filter.
//Based on https://stackoverflow.com/a/42531964
function combinations(array, length) {
const arr = new Array(1 << array.length).fill().map(
(e1, i) => array.filter((e2, j) => i & 1 << j));
return length ? arr.filter(a => a.length == length) : arr;
}
//console.log(combinations([1, 2, 3, 8, 10], 2));
@vasanthv
vasanthv / factorial.js
Last active September 19, 2018 10:02
Get factorial of the given number
const factorial = (n) => (Array.from(Array(n), (_, i) => i + 1)).reduce((a, b) => a * b);
@vasanthv
vasanthv / allCombinationOfArrays.js
Last active May 30, 2019 07:28
A function to get all possible combination of arrays.
const arr = [ [ '8', '0', '5', '7', '9' ], [ '1', '2', '4' ] ]
const output = arr.reduce((a, b) => {
let ret = [];
a.forEach(i => b.forEach(j => ret.push(i + j)));
return ret;
});
// output = [ '81', '82', '84', '01', '02', '04', '51', '52', '54', '71', '72', '74', '91', '92', '94' ];
function isPrime(num) {
if (num > 2 && num % 2 === 0) return false;
for (var i = 3; i < Math.sqrt(num); i += 2) {
if (num % i === 0) return false;
}
return num > 1;
}