javascript cache Function in details

Maintain the caching for the function to faster retrieval of result.

function memoize(func) {
var memo = {};
var slice = Array.prototype.slice;

return function() {
var args = slice.call(arguments);

if (args in memo)
return memo[args];
else
return (memo[args] = func.apply(this, args));

}
}
function add(x,y){
console.log(x+x);
return x+x;
}

function mul(x,y){
console.log(x*x);
return x*x;
}

/*execute below code line by line*/
memoizedAdd1 = memoize(add);
memoizedAdd2 = memoize(mul);
memoizedAdd1(10,10);
memoizedAdd1(20,10);
memoizedAdd1(10,10); /* do not involk the function directly provide stored value in cache due to same argument pass before*/

memoizedAdd2(10,20);
memoizedAdd2(20,20);
memoizedAdd2(10,20); /* do not involk the function directly provide stored value in cache due to same argument pass before*/

Advertisements

javascript : scope and execution context also call and apply advantage

var name = ‘John Doe’;
var data = {
name : ‘herat’,
getName : function(){
return this.name;
},
details : {
name : ‘Herat D’,
getName : function(){
return this.name;
}
}
};

console.log(“data.getName() : “+data.getName());
console.log(“———————————“);
console.log(“data.details.getName() : “+data.details.getName());
console.log(“——————context changes for function which is involked—————“);
console.log(“———————————“);
temp = data.getName;
console.log(“temp = data.getName; temp(); : “+temp());
console.log(“———————————“);
var temp = data.details.getName;
console.log(“temp = data.details.getName temp() : “+temp());
console.log(“—————-Call and apply advantages : this can change the context—————–“);
console.log(“temp.call(data.details) “+temp.call(data.details));
console.log(“———————————“);
console.log(“temp.call(data) “+temp.call(data));

javascript closure basic understanding

/*drawback*/
(function cls(){
x=20;
setTimeout(function(){
console.log(this.x);
},1000);
})();

/*use of clouser */
(function cls(){
x=20;
var _self =this;
setTimeout(function(){
console.log(_self.x);
},1000);
})();

/*scope binding in arrow function is advantage*/
var cls= ()=> {
x=20;
setTimeout(function(){
console.log(this.x);
},1000);
};

execute above code one by one in console.

arrow function has the advantage of scope binding

HTML5 textarea allowed character limit validation

Tags

, , , ,

Always use input event for validation so as whenever key press or up but does not change the text change the content in textarea like cltr / Alt / up / down keys , mouse roller paste in linux ,  that time events like keyup , press will not work

so use input change event every time for input field validation.

also make sure server side validation working

<textarea class=”col-md-12 form-control” rows=”5″ id=”msg” name=”msg”></textarea>

var allowedCount=110;
var textCount=$(“#msg”).val().length;;
$(“#infomsg”).empty().text(allowedCount);

$(“#msg”).on(‘keyup change input’,function(){
textCount = $(“#msg”).val().length;
if(textCount <= allowedCount){
$(“#infomsg”).empty().text((allowedCount-textCount));
}else if(textCount > allowedCount){
$(“#msg”).val($(“#msg”).val().substr(0, allowedCount));
$(“#infomsg”).empty().text($(“#msg”).val().length);
}else{
return false;
}
});

Jquery Dynamic element create and remove creating issue with event listner

Tags

, , ,

If you have used .remove() method for dynamically remove and append element somewhere inside the DOM then event binding will not work on that particular element, for that we can use .detach() method , this will provide you return object of element removed and then you can append that element/Object inside DOM so that event bind on that particular element will remains as it is.

Also please note if you are creating element dynamically than bind jquery event with .on() method.

issue faced during

  • removing DOM element on document.ready()
  • appending it to Other DOM element on document.ready();
  • now attaching event listeners
  • but still event listeners was not working due to .remove() was use
  • i applied fix with .detach()  method

How to find the Username from ip Address in LAN in Windows

Tags

, , , ,

To get another machine’s identity on the LAN network:

  • Open Command (Start > Run > CMD)
  • Type:

NBTSTAT -A IPADDRS

OR

TRACERT IPADDRESS

Response 

VirtualBox Host-Only Network:
Node IpAddress: [0.0.0.0] Scope Id: []

Host not found.

Ethernet:
Node IpAddress: [0.0.0.0] Scope Id: []

Host not found.

Wi-Fi:
Node IpAddress: [192.168.1.193] Scope Id: []

NetBIOS Remote Machine Name Table

Name Type Status
———————————————
AAA-PC <20> UNIQUE Registered
AAA-PC <00> UNIQUE Registered
WORKGROUP <00> GROUP Registered
WORKGROUP <1E> GROUP Registered

MAC Address = 00-00-7D-80-A5-4E

apk file uploading is not allowed in codeigniter

Tags

, , , , ,

We have mentioned the $config[‘allowed_types’]= ‘apk’;

But when i try to upload file on the server it shows me error that “The filetype you are attempting to upload is not allowed.”

To Avoid the error we need place below line inside the application/config/mines.php file

‘apk’ => array(‘application/x-zip’, ‘application/zip’, ‘application/x-zip-compressed’, ‘application/vnd.android.package-archive’, ‘application/octet-stream’),