Skip to content

Instantly share code, notes, and snippets.

View SK-CSE's full-sized avatar
🇮🇳
job( write scalable code || Review PRs );

Saurabh Kumar SK-CSE

🇮🇳
job( write scalable code || Review PRs );
View GitHub Profile
@SK-CSE
SK-CSE / html5-video-streamer.js
Created August 7, 2016 16:33 — forked from paolorossi/html5-video-streamer.js
Node.js HTML5 video streamer
/*
* Inspired by: http://stackoverflow.com/questions/4360060/video-streaming-with-html-5-via-node-js
*/
var http = require('http'),
fs = require('fs'),
util = require('util');
http.createServer(function (req, res) {
var path = 'video.mp4';
@SK-CSE
SK-CSE / basicLogic.js
Last active January 28, 2017 10:25
basic logics
// factorial
function factorial(number) {
var product = 1;
for (var i = 1; i <= number; i++) {
product *= i;
}
return product;
}
// factorial recursive
@SK-CSE
SK-CSE / oops.js
Last active February 7, 2017 17:12
OOPS in JavaScript
// with prototype
function BankAccount(amount) {
this.balance = amount;
}
BankAccount.prototype.deposit = function(amount) {
this.balance += amount;
}
BankAccount.prototype.withdraw = function(amount) {
@SK-CSE
SK-CSE / acronym.js
Created January 28, 2017 09:48
acronym function in javascript
function firstLetter(word) {
return word[0];
};
function getAcronym(str){
var words = str.split(" "); // ["for","your","information"]
var acr = words.map(firstLetter); // ["f","y","i"]
return acr.join("").toUpperCase();
};
@SK-CSE
SK-CSE / filterEvenOdd.js
Last active January 28, 2017 09:57
filter even and odd number in an array
function isEven(num) {
return num % 2 == 0;
}
function isOdd(num) {
return num % 2 != 0;
}
var nums = [];
for (var i = 0; i < 20; ++i) {
nums[i] = i+1;
}
@SK-CSE
SK-CSE / array.js
Last active February 11, 2017 19:04
predefined Array func. in javascript
// The mutator function for adding array elements to the beginning of an array is unshift(). Here is how the function works:
var nums = [2,3,4,5];
console.log(nums); // 2,3,4,5
var newnum = 1;
nums.unshift(newnum);
console.log(nums); // 1,2,3,4,5
// The second call to unshift() demonstrates that you can add multiple elements to an array with one call to the function.
nums = [3,4,5];
nums.unshift(newnum,1,2);
@SK-CSE
SK-CSE / mutidimentionArray.js
Created January 29, 2017 10:06
mutidimention array in javascript
// JavaScript arrays are only one-dimensional, but you can create multidimensional arrays by creating arrays of arrays.
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;
}
@SK-CSE
SK-CSE / operator.md
Last active February 10, 2017 08:10
about operators in javascript

###JavaScript's && operator is coalescing.

js> 0 && 7
0
js> 1 && 7
7

&& does not convert things to Boolean values.

+ operator in string and number

var a = "3"+1+2;
console.log(a); //312

var b = 1+"3"+2;
console.log(b); //132

var c = 1+2+"3";
console.log(c); //33

JSON Stringification

For most simple values, JSON stringification behaves basically the same as toString() conversions, except that the serialization result is always a string:

JSON.stringify( 42 );	// "42"
JSON.stringify( "42" );	// ""42"" (a string with a quoted string value in it)
JSON.stringify( null );	// "null"
JSON.stringify( true );	// "true"

The JSON.stringify(..) utility will automatically omit undefined, function, and symbol values when it comes across them. If such a value is found in an array, that value is replaced by null (so that the array position information isn't altered). If found as a property of an object, that property will simply be excluded.